原文地址

背景

开发过程中遇到问题,简单写个demo 运行环境为Chrome 68

描述一下这个问题,当a标签内部存在嵌套时, 父元素a标签的href默认行为以及子元素绑定的click事件的响应之间存在影响。页面结构:

<!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标签内部点击事件失效</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)">父标签
<span class="child-one">
子标签1
</span>
<object>
<a class="child-two" href="child-two">
子标签2
</a>
</object>
<object>
<a class="child-three" href="javascript:alert('href:child-three')">
子标签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标签嵌套,浏览器解析错误,所以用object标签包裹了一层)。

执行操作:

  1. 当点击父标签时,先弹出111,然后跳转父标签的href链接。
    说明onclick执行先于href
  2. 当点击child-one时,执行元素绑定的click事件,会弹出alert,但是location仍然跳转到了father。
    阻止冒泡后,执行结果仍然不符合预期。添加preventDefault之后,执行了子元素自己的跳转。
  3. 当点击child-two时,弹出响应信息,然后会跳转href的链接。
  4. 当点击child-three时,先弹出click child-three,然后是href child-three,说明click事件先于href执行。

上面4个操作除了2之外都很好理解,2中,为什么已经在阻止了事件冒泡之后,仍然执行了父元素中href的跳转。

思考:

首先可以肯定的是,事件冒泡确实被阻止了,因为父元素的onclick并没有执行。
所以猜测,<a>标签的默认行为是无法通过取消冒泡来阻止的,就算事件没有冒泡到父元素,子元素在父元素<a>标签内部,仍然会执行<a>标签默认行为。

解决方法:

  • 在子元素中添加e.preventDefault()阻止默认行为
  • 父元素不使用<a>标签,使用其他标签绑定click事件且子元素阻止冒泡
  • 父元素不使用href属性,直接在<a>标签上绑定click事件

父元素a标签的href默认行为以及子元素绑定的click事件的响应之间存在影响的更多相关文章

  1. a标签嵌套href默认行为与子元素click事件存在影响

    2018-08-07 Question about work 开发过程中遇到问题,简单写个demo 运行环境为Chrome 68 描述一下这个问题,当<a>标签内部存在嵌套时, 父元素&l ...

  2. EMPTY表示元素不能包含文本,也不能包含子元素

    <?xml version=”1.0″ encoding=”GB2312″?> <!ELEMENT Customer EMPTY> <!ATTLIST Customer称 ...

  3. 父元素高度设置为min-height,子元素高度设置为100%,但实际上子元素高度你知道是多少吗?

    前言 给父元素一个min-height,子元素设置height:100%. 代码 <!DOCTYPE html> <html> <head> <title&g ...

  4. img标签不能直接作为body的子元素

    前几天在一本教材上看到关于HTML标签嵌套规则一节的时候,看到这么一句话,“把图像作为body元素的子元素直接插入到页面中,这样是不妥的,一是结构嵌套有误,二是图像控制不方便.”后面还给了一段代码演示 ...

  5. jQuery编程基础精华02(属性、表单过滤器,元素的each,表单选择器,子元素过滤器(*),追加方法,节点,样式操作)

    属性.表单过滤器 属性过滤选择器: $("div[id]")选取有id属性的<div> $("div[title=test]")选取title属性为 ...

  6. 关于a标签自身的click事件所带来的一些影响

    众所周知a标签自身带有点击事件<a href="#"></a>从它本身的特性来讲并没有什么不好的影响,但是如果你在a标签里又加入onclick事件则< ...

  7. jquery添加的html元素按钮为什么不执行类样式绑定的click事件

    代码举例: 更多按钮: <input type="button" class="addMore" id="addMore${issue.id } ...

  8. 用jq获取元素内文本,但不包括其子元素内的文本值的方法

    <li id="listItem"> This is some text <span id="firstSpan">First span ...

  9. jquery中动态添加的标签绑定的click事件失效的解决办法

    把.click()换成.live('click',function(){})(如果你的jquery的版本是1.10之前) 把.click()换成.on('click',function(){})(jq ...

随机推荐

  1. 给url添加时间戳,解决浏览器缓存

    //解决浏览器缓存function timestamp(url){ // var getTimestamp=Math.random(); var getTimestamp=new Date().get ...

  2. 所有硬币组合问题——动态规划hdu2069

    Problem Description Suppose there are 5 types of coins: 50-cent, 25-cent, 10-cent, 5-cent, and 1-cen ...

  3. git报错-Initial commit Untracked files nothing added to commit but untracked ……

    文章转自 https://www.jianshu.com/p/61c3db30d488 在目标执行命令 git stratus 报错 根据上面的文章,可以解决问题.不行的话,请留言. 感谢你的阅读

  4. Object.entries

    const reduce = Function.bind.call(Function.call, Array.prototype.reduce); const isEnumerable = Funct ...

  5. Spring学习(三)--高级装配

    一.Spring profile 在开发软件的时候,有一个很大的挑战就是将应用程序从一个环境迁 移到另外一个环境.开发阶段中,某些环境相关做法可能并不适合迁 移到生产环境中,甚至即便迁移过去也无法正常 ...

  6. 使用requests_html抓取数据

    from requests_html import HTMLSession import json class YejiCollege: def __init__(self, url): self.u ...

  7. [暑假集训Day3T2]骑士问题

    标准的广搜. 采用队列保存形态,如果不会广搜的可以多看看PJ知识点.由于输入多组数据,每次标记数组要清空,每次队列元素也都要清空. 参考代码如下: #include<iostream> # ...

  8. JS中对象数据类型的基本结构和操作

    Object类型 ECMAScript中的队形其实就是一组数据和功能的集合.对象可以通过执行new操作符后跟要创建的对象类型的名称来创建.而创建Object类型的示例并为其添加属性和(或)方法,就可以 ...

  9. YouCompleteMe报错可能是第三方库没有

    git submodule update --init --recursive 到YouCompleteMe安装目录下,执行上面的命令

  10. AI-Tensorflow-神经网络优化算法-梯度下降算法-学习率

    记录内容来自<Tensorflow实战Google一书>及MOOC人工智能实践 http://www.icourse163.org/learn/PKU-1002536002?tid=100 ...