欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vue常用事件v-on:click詳解事件對象,事件冒泡,事件默認行為

 更新時間:2022年08月19日 11:05:04   作者:傻小胖  
這篇文章主要介紹了vue常用事件之v-on:click?以及事件對象,事件冒泡,事件默認行為,其實v-on后面跟的不止是click事件也可以是其他的事件,用法均相似,本文通過實例代碼給大家介紹的非常詳細,需要的朋友可以參考下

其實v-on后面跟的不止是click事件也可以是其他的事件,用法均相似。比如:v-on:click/mouseout/mouseover/mousedown.......

以下click為例

注意:所有的v-on都可以簡寫為@,比如說v-click可以簡寫為@click

1.監(jiān)聽事件

可以用v-on指令監(jiān)聽 DOM 事件,并在觸發(fā)時運行一些 JavaScript 代碼。通常來講就是監(jiān)聽dom觸發(fā)一些操作,這些操作(比如點擊)觸發(fā)后執(zhí)行的動作(js)可有直接寫在后面

v-on:click="item+=1"

eg:

<template>
  <div >
   <input type="button" value="clickme" v-on:click="item+=1"/>
    <div>{{item}}</div>
  </div>
</template>
 
<script>
export default {
  name: 'HelloWorld',
  data () {
    return {
    item:1
    }
  }
}
</script>
 
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
 
</style>

結(jié)果:

可以看見每點擊一次綁定的值就增加1.也就是說可以吧js的操作放在事件觸發(fā)的后面。但是有時候邏輯太復雜的時候?qū)懺诶锩婢蜁斐苫靵y,視圖和邏輯混淆。所以click后面可以接一個方法,把所有處理邏輯的方法封裝在一個函數(shù)里面click的時候調(diào)用

2.事件處理方法

v-on:click="greet"

eg;

<template>
  <div >
   <input type="button" value="clickme" v-on:click="greet"/>
    <div>{{res}}</div>
  </div>
</template>
 
<script>
export default {
  name: 'HelloWorld',
  data () {
    return {
      name : 1,
      res:""
    }
  },
  methods:{
    greet: function () {
      // `this` 在方法里指向當前 Vue 實例
      this.res='Hello ' + this.name + '!';
    }
  }
}
</script>
 
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
 
</style>

效果:

可以看見點擊之后執(zhí)行了greet方法里面js邏輯

3.帶參數(shù)的時間綁定方法:

同上,唯一區(qū)別是攜帶了參數(shù)

 v-on:click="greet(name)"
<template>
  <div >
   <input type="button" value="clickme" v-on:click="greet(name)"/>
    <div>{{res}}</div>
  </div>
</template>
 
<script>
export default {
  name: 'HelloWorld',
  data () {
    return {
      name : 1,
      res:""
    }
  },
  methods:{
    greet: function (reccept) {
      // `this` 在方法里指向當前 Vue 實例
      this.res='Hello ' + reccept+1 + '!';
    }
  }
}
</script>
 
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
 
</style>

效果一致。對方法的調(diào)用同樣可以一個方法多處多次的調(diào)用

4.內(nèi)聯(lián)處理器中的方法

也就是說在方法里面調(diào)用其他的方法,這里的其他方法可以是js原生的方法比如阻止冒泡呀等等,也可以是自定義的方法

v-on:click="greet(name,$event)"

eg:

<template>
  <div >
   <input type="button" value="clickme" v-on:click="greet(name,$event)"/>
    <div>{{res}}</div>
  </div>
</template>
 
<script>
export default {
  name: 'HelloWorld',
  data () {
    return {
      name : 1,
      res:""
    }
  },
  methods:{
    greet: function (reccept,event) {
      if (reccept===1) this.say()
    },
    say:function () {
      this.res="我調(diào)用了"
    }
  }
}
</script>
 
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
 
</style>

效果:

5.事件對象

$event 拿到當前點擊事件的事件對象,比如click就是拿到當前點擊的dom事件對象信息

v-on:click="greet($event)"

eg:

<template>
  <div >
   <input type="button" value="clickme" v-on:click="greet($event)"/>
  </div>
</template>
 
<script>
export default {
  name: 'HelloWorld',
  data () {
    return {
      hide : true
    }
  },
  methods:{
    greet: function (ev) {
    alert(ev.clientX)
    }
  }
}
</script>
 
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
 
</style>
v-on:click="greet($event)"/>
  </div>
</template>

<script>
export default {
  name: 'HelloWorld',
  data () {
    return {
      hide : true
    }
  },
  methods:{
    greet: function (ev) {
    alert(ev.clientX)
    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>

</style>
v-on:click="greet($event)"/>
  </div>
</template>

<script>
export default {
  name: 'HelloWorld',
  data () {
    return {
      hide : true
    }
  },
  methods:{
    greet: function (ev) {
    alert(ev.clientX)
    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>

</style>

效果:

6.事件冒泡

當不阻止事件冒泡的時候會彈兩次

eg

<template>
  <div >
    <div @click="show1($event)">
      <div @click="show2($event)">點擊我呀</div>
    </div>
  </div>
</template>
 
<script>
export default {
  name: 'HelloWorld',
  data () {
    return {
      hide : true
    }
  },
  methods:{
    show1: function (ev) {
     alert(1)
    },
    show2: function (ev1) {
      alert(2)
    }
  }
}
</script>
 
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
 
</style>

那么但阻止冒泡后就只會彈一次

eg:原生js阻止冒泡

 ev1.cancelBubble=true
<template>
  <div >
    <div @click="show1($event)">
      <div @click="show2($event)">點擊我呀</div>
    </div>
  </div>
</template>
 
<script>
export default {
  name: 'HelloWorld',
  data () {
    return {
      hide : true
    }
  },
  methods:{
    show1: function (ev) {
     alert(1)
    },
    show2: function (ev1) {
        ev1.cancelBubble=true
      alert(2)
 
    }
  }
}
</script>
 
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
 
</style>

那么vue自己封裝的阻止冒泡方法呢?

@click.stop="show2()"

eg:

<template>
  <div >
    <div @click="show1()">
      <div @click.stop="show2()">點擊我呀</div>
    </div>
  </div>
</template>
 
<script>
export default {
  name: 'HelloWorld',
  data () {
    return {
      hide : true
    }
  },
  methods:{
    show1: function () {
     alert(1)
    },
    show2: function (ev1) {
      alert(2)
    }
  }
}
</script>
 
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
 
</style>

7.阻止默認行為:

比如:如下右鍵之后會將默認的菜單帶出來

<template>
  <div >
    <div>
      <div @contextmenu="show2()">右鍵點擊我呀</div>
    </div>
  </div>
</template>
 
<script>
export default {
  name: 'HelloWorld',
  data () {
    return {
      hide : true
    }
  },
  methods:{
    show2: function (ev1) {
      alert(2)
 
    }
  }
}
</script>
 
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
 
</style>

效果:

那么就有了阻止默認行為

 ev1.preventDefault();

eg:

<template>
  <div >
    <div>
      <div @contextmenu="show2($event)">右鍵點擊我呀</div>
    </div>
  </div>
</template>
 
<script>
export default {
  name: 'HelloWorld',
  data () {
    return {
      hide : true
    }
  },
  methods:{
    show2: function (ev1) {
      alert(2);
      ev1.preventDefault();
    }
  }
}
</script>
 
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
 
</style>

點擊后默認菜單將不會顯示(PS早360瀏覽器右鍵無效)

vue里面的封裝的阻止默認行為的方法:

@contextmenu.prevent="show2()"

eg:

<template>
  <div >
    <div>
      <div @contextmenu.prevent="show2()">右鍵點擊我呀</div>
    </div>
  </div>
</template>
 
<script>
export default {
  name: 'HelloWorld',
  data () {
    return {
      hide : true
    }
  },
  methods:{
    show2: function (ev1) {
      alert(2);
    }
  }
}
</script>
 
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
 
</style>

8.其他事件修飾符

用法都一樣就不再贅述

  • .capture
  • .self
  • .once
<!-- 阻止單擊事件繼續(xù)傳播 -->
<a v-on:click.stop="doThis"></a>
<!-- 提交事件不再重載頁面 -->
<form v-on:submit.prevent="onSubmit"></form>
<!-- 修飾符可以串聯(lián) -->
<a v-on:click.stop.prevent="doThat"></a>
<!-- 只有修飾符 -->
<form v-on:submit.prevent></form>
<!-- 添加事件監(jiān)聽器時使用事件捕獲模式 -->
<!-- 即元素自身觸發(fā)的事件先在此處處理,然后才交由內(nèi)部元素進行處理 -->
<div v-on:click.capture="doThis">...</div>
<!-- 只當在 event.target 是當前元素自身時觸發(fā)處理函數(shù) -->
<!-- 即事件不是從內(nèi)部元素觸發(fā)的 -->
<div v-on:click.self="doThat">...</div>

使用修飾符時,順序很重要;相應的代碼會以同樣的順序產(chǎn)生。因此,用@click.prevent.self會阻止所有的點擊,而@click.self.prevent只會阻止對元素自身的點擊。

2.1.4 新增

<!-- 點擊事件將只會觸發(fā)一次 -->
<a v-on:click.once="doThis"></a>

不像其它只能對原生的 DOM 事件起作用的修飾符,.once修飾符還能被用到自定義的組件事件上。如果你還沒有閱讀關(guān)于組件的文檔,現(xiàn)在大可不必擔心。

<!-- the scroll event will not cancel the default scroll behavior -->
<div v-on:scroll.passive="onScroll">...</div>

Vue 為這些修飾符額外提供了.passive修飾符來提升移動端的性能。舉個例子,在滾動的時候,瀏覽器會在整個事件處理完畢之后再觸發(fā)滾動,因為瀏覽器并不知道這個事件是否在其處理函數(shù)中被調(diào)用了event.preventDefault()。.passive修飾符用來進一步告訴瀏覽器這個事件的默認行為不會被取消。

不要把.passive.prevent一起使用。被動處理函數(shù)無法阻止默認的事件行為。

補充:原生JS阻止冒泡

來自網(wǎng)友評論貢獻~~
event.cancelBubble=true ie9以下;
event.stoppropagation();主流瀏覽器 IE9及以上,
原生JS阻止默認事件:
event.preventDefault();主流
event.returnValue = false;IE

到此這篇關(guān)于vue常用事件之v-on:click以及事件對象,事件冒泡,事件默認行為的文章就介紹到這了,更多相關(guān)vuev-on:click內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • vue vant中picker組件的使用

    vue vant中picker組件的使用

    這篇文章主要介紹了vue vant中picker組件的使用說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-11-11
  • vue3如何實現(xiàn)PDF文件在線預覽功能

    vue3如何實現(xiàn)PDF文件在線預覽功能

    PDF文件在線預覽的功能相信大家都是有遇到過的,下面這篇文章主要給大家介紹了關(guān)于vue3如何實現(xiàn)PDF文件在線預覽功能的相關(guān)資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2022-06-06
  • VUE + UEditor 單圖片跨域上傳功能的實現(xiàn)方法

    VUE + UEditor 單圖片跨域上傳功能的實現(xiàn)方法

    這篇文章主要介紹了VUE + UEditor 單圖片跨域上傳功能的實現(xiàn)方法,需要的朋友參考下
    2018-02-02
  • 淺談vuex 閑置狀態(tài)重置方案

    淺談vuex 閑置狀態(tài)重置方案

    本篇文章主要介紹了vuex 閑置狀態(tài)重置方案,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-01-01
  • vue去掉嚴格開發(fā),去掉vue-cli安裝時的eslint或修改配置方式

    vue去掉嚴格開發(fā),去掉vue-cli安裝時的eslint或修改配置方式

    這篇文章主要介紹了vue去掉嚴格開發(fā),去掉vue-cli安裝時的eslint或修改配置方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-04-04
  • vue項目部署到Apache服務(wù)器中遇到的問題解決

    vue項目部署到Apache服務(wù)器中遇到的問題解決

    這篇文章主要介紹了vue項目部署到Apache中遇到的問題解決,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-08-08
  • Vue3實現(xiàn)圖片放大鏡效果

    Vue3實現(xiàn)圖片放大鏡效果

    這篇文章主要為大家詳細介紹了Vue3實現(xiàn)圖片放大鏡效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • Vue前端書寫規(guī)范大全(非常詳細!)

    Vue前端書寫規(guī)范大全(非常詳細!)

    在團體開發(fā)項目中,為了團隊所有成員書寫可維護的代碼,而不是一次性的代碼,讓團隊當中其他人看你的代碼能一目了然,下面這篇文章主要給大家介紹了關(guān)于Vue前端書寫規(guī)范的相關(guān)資料,需要的朋友可以參考下
    2023-01-01
  • vue項目中實現(xiàn)多文件上傳功能實例代碼

    vue項目中實現(xiàn)多文件上傳功能實例代碼

    我們平時經(jīng)常做的是上傳文件,下面這篇文章主要給大家介紹了關(guān)于vue項目中實現(xiàn)多文件上傳功能的相關(guān)資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2022-11-11
  • vue?router?動態(tài)路由清除方式

    vue?router?動態(tài)路由清除方式

    這篇文章主要介紹了vue?router?動態(tài)路由清除方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-05-05

最新評論