原文地址javascript
开发过程当中遇到问题,简单写个demo 运行环境为Chrome 68html
描述一下这个问题,当a标签内部存在嵌套时, 父元素a标签的href默认行为以及子元素绑定的click事件的响应之间存在影响。页面结构:java
<!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标签包裹了一层)。git
preventDefault
以后,执行了子元素本身的跳转。click child-three
,而后是href child-three
,说明click事件先于href执行。上面4个操做除了2以外都很好理解,2中,为何已经在阻止了事件冒泡以后,仍然执行了父元素中href
的跳转。github
首先能够确定的是,事件冒泡确实被阻止了,由于父元素的onclick并无执行。
因此猜想,<a>
标签的默认行为是没法经过取消冒泡来阻止的,就算事件没有冒泡到父元素,子元素在父元素<a>
标签内部,仍然会执行<a>
标签默认行为。浏览器
e.preventDefault()
阻止默认行为<a>
标签,使用其余标签绑定click事件且子元素阻止冒泡href
属性,直接在<a>
标签上绑定click事件