gpt4 book ai didi

javascript - 为什么 e.preventDefault() 不会阻止链接被跟踪?

转载 作者:行者123 更新时间:2023-11-28 15:55:29 24 4
gpt4 key购买 nike

如何阻止链接被跟踪(使用此事件处理程序)?

http://jsfiddle.net/chovy/rsqH7/1/

<table>
<tbody>
<tr class="msg">
<header><a href="http://cnn.com">cnn.com</a></header></tr>
</tbody>
</table>

$('table').on('click', 'tr.msg header', function (e) {
e.preventDefault();
var $el = $(e.currentTarget);
console.log($el);
});

最佳答案

您的 HTML 无效; <header>不能直接出现在 <tr> 中,这打破了整个事情。如果添加 alert ,您会注意到处理程序根本没有被调用。

检查 DOM 给了我这个:

<header><a href="http://cnn.com">cnn.com</a></header>
<table>…</table>

这就是浏览器,一如既往的有用! Correcting the HTML by putting adding a <td> fixes it. (或者您的意思是 <th> 而不是 <header> ?)

关于javascript - 为什么 e.preventDefault() 不会阻止链接被跟踪?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19072775/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com