溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

父元素<a>標(biāo)簽的默認(rèn)行為以及click事件之間有什么相互關(guān)系

發(fā)布時(shí)間:2020-07-09 16:05:18 來源:億速云 閱讀:191 作者:Leah 欄目:web開發(fā)

本篇文章給大家分享的是有關(guān)父元素<a>標(biāo)簽的默認(rèn)行為以及click事件之間有什么相互關(guān)系,小編覺得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。

開發(fā)過程中遇到問題,簡單寫個(gè)demo   運(yùn)行環(huán)境為Chrome 68

描述一下這個(gè)問題,當(dāng)a標(biāo)簽內(nèi)部存在嵌套時(shí), 父元素a標(biāo)簽的href默認(rèn)行為以及子元素綁定的click事件的響應(yīng)之間存在影響。頁面結(jié)構(gòu):

<!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>a標(biāo)簽內(nèi)部點(diǎn)擊事件失效</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        .father {
            display: block;
            width: 500px;
            height: 200px;
            background-color: rgb(210, 111, 30);
        }

        .child-one {
            display: block;
            width: 200px;
            height: 50px;
            background-color: rgb(174, 43, 226);
        }

        .child-two {
            display: block;
            width: 200px;
            height: 50px;
            background-color: rgb(43, 226, 67);
        }

        .child-three {
            display: block;
            width: 200px;
            height: 50px;
            background-color: rgb(43, 137, 226);
        }
    </style>
</head>

<body>
    <a class="father" href="father" onclick="alert(111)">父標(biāo)簽
        <span class="child-one">
            子標(biāo)簽1
        </span>
        <object>
            <a class="child-two" href="child-two">
                子標(biāo)簽2
            </a>
        </object>
        <object>
            <a class="child-three" href="javascript:alert('href:child-three')">
                子標(biāo)簽3
            </a>
        </object>
    </a>
    <script>    
        let father = document.querySelector('.father');
        let ele1 = document.querySelector('.child-one');
        let ele2 = document.querySelector('.child-two');
        let ele3 = document.querySelector('.child-three');

        ele1.addEventListener('click', function (e) {
            e.stopPropagation();
            // e.preventDefault();
            alert('click child-one')
            window.location.href = 'child-one'
        }, false)

        ele2.addEventListener('click', function (e) {
            e.stopPropagation();
            alert('click child-two')
            // window.location.href='child-two'
        }, false)

        ele3.addEventListener('click', function (e) {
            alert('click child-three')
            window.location.href = 'child-three'
        }, false)

        father.addEventListener('click', function (e) {
            alert('click father')
            window.location.href = 'father'
        }, false)

    </script>
</body>

</html>

示例如下圖(如果a標(biāo)簽嵌套,瀏覽器解析錯(cuò)誤,所以用object標(biāo)簽包裹了一層)。

父元素<a>標(biāo)簽的默認(rèn)行為以及click事件之間有什么相互關(guān)系

執(zhí)行操作:

  1. 當(dāng)點(diǎn)擊父標(biāo)簽時(shí),先彈出111,然后跳轉(zhuǎn)父標(biāo)簽的href鏈接。
    說明onclick執(zhí)行先于href

  2. 當(dāng)點(diǎn)擊child-one時(shí),執(zhí)行元素綁定的click事件,會彈出alert,但是location仍然跳轉(zhuǎn)到了father。
    阻止冒泡后,執(zhí)行結(jié)果仍然不符合預(yù)期。添加preventDefault之后,執(zhí)行了子元素自己的跳轉(zhuǎn)。

  3. 當(dāng)點(diǎn)擊child-two時(shí),彈出響應(yīng)信息,然后會跳轉(zhuǎn)href的鏈接。

  4. 當(dāng)點(diǎn)擊child-three時(shí),先彈出click child-three,然后是href child-three,說明click事件先于href執(zhí)行。

上面4個(gè)操作除了2之外都很好理解,2中,為什么已經(jīng)在阻止了事件冒泡之后,仍然執(zhí)行了父元素中href的跳轉(zhuǎn)。

思考:

首先可以肯定的是,事件冒泡確實(shí)被阻止了,因?yàn)楦冈氐膐nclick并沒有執(zhí)行。
所以猜測,<a>標(biāo)簽的默認(rèn)行為是無法通過取消冒泡來阻止的,就算事件沒有冒泡到父元素,子元素在父元素<a>標(biāo)簽內(nèi)部,仍然會執(zhí)行<a>標(biāo)簽?zāi)J(rèn)行為。

解決方法:

在子元素中添加e.preventDefault()阻止默認(rèn)行為

父元素不使用<a>標(biāo)簽,使用其他標(biāo)簽綁定click事件且子元素阻止冒泡

父元素不使用href屬性,直接在<a>標(biāo)簽上綁定click事件

相關(guān)文章推薦:

link標(biāo)簽鏈接CSS和@import加載有什么區(qū)別?

HTML標(biāo)簽:img標(biāo)簽的用法總結(jié)

以上就是父元素<a>標(biāo)簽的默認(rèn)行為以及click事件之間有什么相互關(guān)系,小編相信有部分知識點(diǎn)可能是我們?nèi)粘9ぷ鲿姷交蛴玫降?。希望你能通過這篇文章學(xué)到更多知識。更多詳情敬請關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI