VUE事件處理

.stop 停止事件冒泡
.prevent 阻止事件默認(rèn)行為

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <div id="demo">
        <h2>綁定監(jiān)聽</h2>
            <button @click="test1">test1</button>
            <button @click="test2('a')">test1</button>
            <button @click="test3($event)">test1</button>
            <button @click="test4(123,$event)">test1</button>
        <h2>事件修飾符</h2>

        <div style="width: 200px;height: 200px;background-color: red" @click="test5">
            <div style="width: 100px;height: 100px;background-color: blue" @click.stop="test6"></div>
        </div>
        <a href="www.baidu.com" @click.prevent="test7">去百度</a>
        <h2>按鍵修飾符</h2>
        <input type="text" @keyup.13="test8">
        <input type="text" @keyup.enter="test8">
    </div>
</body>
<script src="../js/vue.js"></script>
<script>
    new Vue({
        el:'#demo',
        data:{
            test1(){
                alert('test1');
            },
            test2(msg){
                alert(msg);
            },
            test3(event){
                alert(event.target.innerHTML);
            },
            test4(number,event){
                alert(number + '---' + event.target.innerHTML);
            },
            test5(){
                alert('out');
            },
            test6(event){
                event.stopPropagation();//阻止事件冒泡
                alert('inner');
            },
            test7(event){
                event.preventDefault();//阻止事件的默認(rèn)行為
                alert('點擊');
            },
            test8(event){
                alert(event.target.value);
            }
        }
    })
</script>
</html>
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 綁定監(jiān)聽:v-on:xxx="fun"@xxx="fun"@xxx="fun(參數(shù))"默認(rèn)事件形參: event隱...
    牛耀閱讀 216評論 0 0
  • 監(jiān)聽事件 可以用 v-on 指令監(jiān)聽 DOM 事件,并在觸發(fā)時運行一些 JavaScript 代碼。 效果: 事件...
    Program_黑閱讀 1,219評論 2 7
  • ??Vue事件監(jiān)聽的方式貌似違背了關(guān)注點分離(separation of concern)的傳統(tǒng)理念。實際上,所有...
    小小的開發(fā)人員閱讀 2,644評論 0 0
  • 1)v-on:click="greet"可以簡寫成@click="greet" 2)click事件的一些屬性 v-...
    puxiaotaoc閱讀 241評論 0 0
  • v-on:eventName 綁定事件 事件修飾符 對應(yīng)原生JS .prevent阻止默認(rèn)事件-----.prev...
    Gino_Li閱讀 106評論 0 0

友情鏈接更多精彩內(nèi)容