最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Vue中的event對象介紹

 更新時間:2022年03月14日 09:22:13   作者:.NET開發(fā)菜鳥  
這篇文章介紹了Vue中的event對象,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下

一、什么是event對象

event對象:代表的是事件的狀態(tài)。比如獲取當前的元素:e.Target。

二、事件冒泡

什么是事件冒泡呢?百度百科的解釋如下:

當事件發(fā)生后,這個事件就要開始傳播(從里到外或者從外向里)。為什么要傳播呢?因為事件源本身(可能)并沒有處理事件的能力,即處理事件的函數(方法)并未綁定在該事件源上。例如我們點擊一個按鈕時,就會產生一個click事件,但這個按鈕本身可能不能處理這個事件,事件必須從這個按鈕傳播出去,從而到達能夠處理這個事件的代碼中(例如我們給按鈕的onclick屬性賦一個函數的名字,就是讓這個函數去處理該按鈕的click事件),或者按鈕的父級綁定有事件函數,當該點擊事件發(fā)生在按鈕上,按鈕本身并無處理事件函數,則傳播到父級去處理。

可能下面的例子會更容易理解一些:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>事件冒泡</title>
    <!--引入vue.js-->
    <script src="node_modules/vue/dist/vue.js" ></script>
    <script>
       window.onload=function(){
           // 構建vue實例
           new Vue({
               el:"#my",
               data:{
               },
               // 方法
               methods:{
                   play1:function(){
                       console.log("我的div1");
                   },
                   play2:function(){
                       console.log("我的div2");
                   },
                   play3:function(){
                       console.log("我的div3");
                   }
               }
           })
       }
    </script>
</head>
<body>
    <div id="my">
        <div @click="play1">我的div1
            <div @click="play2">我的div2
                <div @click="play3">
                    我的div3
                </div>
            </div>
        </div>
    </div>
</body>
</html>

效果:

在上面的代碼中,3個div分別綁定了3個不同的事件,點擊"我的div3"的時候

那么該如何阻止事件冒泡呢?

1、原始JS中的處理方法

代碼示例如下:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>事件冒泡</title>
    <!--引入vue.js-->
    <script src="node_modules/vue/dist/vue.js" ></script>
    <script>
       window.onload=function(){
           // 構建vue實例
           new Vue({
               el:"#my",
               data:{
               },
               // 方法
               methods:{
                   play1:function(){
                       console.log("我的div1");
                   },
                   play2:function(){
                       console.log("我的div2");
                   },
                   play3:function(e){
                       console.log("我的div3");
                       e.stopPropagation();
                   }
               }
           })
       }
    </script>
</head>
<body>
    <div id="my">
        <div @click="play1">我的div1
            <div @click="play2">我的div2
                <div @click="play3($event)">
                    我的div3
                </div>
            </div>
        </div>
    </div>
</body>
</html>

效果:

2、vue中處理方法

代碼示例如下:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>事件冒泡</title>
    <!--引入vue.js-->
    <script src="node_modules/vue/dist/vue.js" ></script>
    <script>
       window.onload=function(){
           // 構建vue實例
           new Vue({
               el:"#my",
               data:{
               },
               // 方法
               methods:{
                   play1:function(){
                       console.log("我的div1");
                   },
                   play2:function(){
                       console.log("我的div2");
                   },
                   play3:function(e){
                       console.log("我的div3");
                       //e.stopPropagation();
                   }
               }
           })
       }
    </script>
</head>
<body>
    <div id="my">
        <div @click="play1">我的div1
            <div @click="play2">我的div2
                <div @click="play3($event)">
                    我的div3
                </div>
                <!--Vue中使用事件修飾符阻止冒泡-->
                <div @click.stop="play3($event)">
                    我的div4
                </div>
            </div>
        </div>
    </div>
</body>
</html>

效果:

點擊"我的div4"的時候會阻止事件冒泡,但點擊"我的div3"的時候不會阻止事件冒泡。

三、事件的默認動作

看下面的代碼示例:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>事件冒泡</title>
    <!--引入vue.js-->
    <script src="node_modules/vue/dist/vue.js" ></script>
    <script>
       window.onload=function(){
           // 構建vue實例
           new Vue({
               el:"#my",
               data:{
               },
               // 方法
               methods:{
                   play1:function(){
                       console.log("我的div1");
                   },
                   play2:function(){
                       console.log("我的div2");
                   },
                   play3:function(e){
                       console.log("我的div3");
                       //e.stopPropagation();
                   },
                   play4:function(e){
                       console.log("我是超鏈接");
                   }
               }
           })
       }
    </script>
</head>
<body>
    <div id="my">
        <div @click="play1">我的div1
            <div @click="play2">我的div2
                <div @click="play3($event)">
                    我的div3
                </div>
                <!--Vue中使用事件修飾符阻止冒泡-->
                <div @click.stop="play3($event)">
                    我的div4
                </div>

                <a  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  @click="play4($event)">click</a>
            </div>
        </div>
    </div>
</body>
</html>

效果:

點擊“click”的時候會發(fā)現頁面跳轉到了百度,不會進入play4事件,如果調試代碼想進入play4事件該如何處理呢?

1、使用原生JS處理

代碼示例如下:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>事件冒泡</title>
    <!--引入vue.js-->
    <script src="node_modules/vue/dist/vue.js" ></script>
    <script>
       window.onload=function(){
           // 構建vue實例
           new Vue({
               el:"#my",
               data:{
               },
               // 方法
               methods:{
                   play1:function(){
                       console.log("我的div1");
                   },
                   play2:function(){
                       console.log("我的div2");
                   },
                   play3:function(e){
                       console.log("我的div3");
                       //e.stopPropagation();
                   },
                   play4:function(e){
                       console.log("我是超鏈接");
                       // 取消事件的默認動作
                       e.preventDefault();
                   }
               }
           })
       }
    </script>
</head>
<body>
    <div id="my">
        <div @click="play1">我的div1
            <div @click="play2">我的div2
                <div @click="play3($event)">
                    我的div3
                </div>
                <!--Vue中使用事件修飾符阻止冒泡-->
                <div @click.stop="play3($event)">
                    我的div4
                </div>

                <a  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  @click="play4($event)">click</a>
            </div>
        </div>
    </div>
</body>
</html>

效果:

這里在點擊“click”的時候就不會進入百度首頁了。這里沒有處理冒泡,所以會觸發(fā)play2和play1事件。

2、使用vue處理

代碼示例如下:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>事件冒泡</title>
    <!--引入vue.js-->
    <script src="node_modules/vue/dist/vue.js" ></script>
    <script>
       window.onload=function(){
           // 構建vue實例
           new Vue({
               el:"#my",
               data:{
               },
               // 方法
               methods:{
                   play1:function(){
                       console.log("我的div1");
                   },
                   play2:function(){
                       console.log("我的div2");
                   },
                   play3:function(e){
                       console.log("我的div3");
                       //e.stopPropagation();
                   },
                   play4:function(e){
                       console.log("我是超鏈接");
                       // 取消事件的默認動作
                       //e.preventDefault();
                   }
               }
           })
       }
    </script>
</head>
<body>
    <div id="my">
        <div @click="play1">我的div1
            <div @click="play2">我的div2
                <div @click="play3($event)">
                    我的div3
                </div>
                <!--Vue中使用事件修飾符阻止冒泡-->
                <div @click.stop="play3($event)">
                    我的div4
                </div>

                <a  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  @click="play4($event)">click</a>
                <!--使用vue處理-->
                <a  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  @click.prevent.stop="play4($event)">click2</a>
            </div>
        </div>
    </div>
</body>
</html>

效果:

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • vue3中事件處理@click的用法詳解

    vue3中事件處理@click的用法詳解

    @click指令用于監(jiān)聽元素的點擊事件,并在觸發(fā)時執(zhí)行相應的處理函數,在Vue3中,事件處理就可以通過@click指令來實現,下面我們就來看看如何在Vue3中處理點擊事件吧
    2023-08-08
  • Vxe-Table開發(fā)中的各種坑以及避坑指南

    Vxe-Table開發(fā)中的各種坑以及避坑指南

    vxe-table是一個全功能的Vue表格,滿足絕大部分對Table的一切需求,與任意組件庫完美兼容,下面這篇文章主要給大家介紹了關于Vxe-Table開發(fā)中各種坑以及避坑的相關資料,需要的朋友可以參考下
    2022-09-09
  • Vue EventBus自定義組件事件傳遞

    Vue EventBus自定義組件事件傳遞

    這篇文章主要介紹了Vue EventBus自定義組件事件傳遞,組件化應用構建是Vue的特點之一,本文主要介紹EventBus進行數據消息傳遞 ,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-06-06
  • Vue.js路由實現選項卡簡單實例

    Vue.js路由實現選項卡簡單實例

    這篇文章主要為大家詳細介紹了Vue.js路由實現選項卡簡單實例,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-07-07
  • Vue3中slot插槽基本使用

    Vue3中slot插槽基本使用

    插槽slot可以說在一個Vue項目里面處處都有它的身影,比如我們使用一些UI組件庫的時候,我們通??梢允褂貌宀蹃碜远x我們的內容,這篇文章主要介紹了Vue3中slot插槽使用方式,需要的朋友可以參考下
    2022-08-08
  • 淺析Vue3中Excel下載模板并導入數據功能的實現

    淺析Vue3中Excel下載模板并導入數據功能的實現

    這篇文章主要為大家詳細介紹了Vue3中的Excel數據管理,即下載模板并導入數據功能的實現,文中的示例代碼講解詳細,感興趣的小伙伴可以參考一下
    2024-05-05
  • Vue表單驗證?trigger:'blur'OR?trigger:'change'區(qū)別解析

    Vue表單驗證?trigger:'blur'OR?trigger:'change&ap

    利用?elementUI?實現表單元素校驗時,出現下拉框內容選中后校驗不消失的異常校驗情形,這篇文章主要介紹了Vue表單驗證?trigger:‘blur‘?OR?trigger:‘change‘?區(qū)別,需要的朋友可以參考下
    2023-09-09
  • vue-treeselect無法點擊問題(點擊無法出現拉下菜單)

    vue-treeselect無法點擊問題(點擊無法出現拉下菜單)

    這篇文章主要介紹了vue-treeselect無法點擊問題(點擊無法出現拉下菜單),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-04-04
  • elementUI table如何給表頭添加氣泡顯示

    elementUI table如何給表頭添加氣泡顯示

    這篇文章主要介紹了elementUI table如何給表頭添加氣泡顯示問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-11-11
  • vue中解決el-date-picker更改樣式不生效問題

    vue中解決el-date-picker更改樣式不生效問題

    在使用Vue.js進行前端開發(fā)的過程中,Element?UI?是一個非常流行的UI庫,它提供了一套完整的組件來快速搭建美觀的用戶界面,但是我們經常遇到一個問題使用Element?UI提供的el-date-picker組件時,嘗試自定義其樣式卻無法生效,所以本文給大家介紹如何解決這個問題
    2024-10-10

最新評論

邯郸县| 内黄县| 大新县| 丹江口市| 于都县| 南溪县| 楚雄市| 亚东县| 财经| 嘉荫县| 祁东县| 常宁市| 两当县| 韶山市| 长汀县| 仲巴县| 沙洋县| 绥德县| 定结县| 田阳县| 乳山市| 东平县| 汾阳市| 苍溪县| 东源县| 平潭县| 刚察县| 南京市| 德江县| 义乌市| 奉节县| 交口县| 宜春市| 荔波县| 越西县| 宿迁市| 古田县| 手游| 漳州市| 呈贡县| 苏尼特左旗|