父元素<a>标签的默许行为以及click事件之间的彼此影响
开发历程中碰到问题,简便写个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标签包裹了一层)。
施行操纵:
当点击父标签时,先弹出111,然后跳转父标签的href链接。
说明onclick施行先于href当点击child-one时,施行元素绑定的click事件,会弹出alert,但是location依然跳转到了father。
阻挠冒泡后,施行结果依然不相符预测。增加preventDefault
之后,施行了子元素本人的跳转。当点击child-two时,弹出响应信息,然后会跳转href的链接。
当点击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事件
相关文章引荐:
link标签链接CSS和@import加载有什么不同?
HTML标签:img标签的用途总结
以上就是父元素<a>标签的默许行动乃至click事件之间的彼此影响的具体内容,更多请关注百分百源码网其它相关文章!