gpt4 book ai didi

javascript - 链接点击事件被父 mousedown 事件阻止

转载 作者:行者123 更新时间:2023-11-30 16:17:18 25 4
gpt4 key购买 nike

我在这样的标题中有一个链接

<table id="mytable" border=1>
<thead>
<th id='myHeader'><a href="#" class="sortHeader">HelloWorld</a></th>
</thead>
</table>

还有一些示例 javascript,在链接上连接点击事件,在表头元素上连接鼠标移动事件。

var randomFunction = function(event, id){
alert('header was moved');
}

$('#mytable').on('click', '.sortHeader', function(evt){
alert('header was clicked')
});

var header = document.getElementById('myHeader');
header.addEventListener('mousedown', randomFunction);

不幸的是,当我添加 mousedown 时,单击事件不再由单击触发。我已经检查了谷歌开发者工具中的事件,它们都在那里。如何触发点击事件?

这是一个完整的 jsfiddle:https://jsfiddle.net/nickwinters/xLz8agy0/1/

最佳答案

您需要使用 console.log(); 而不是 alert()

因为在鼠标按下时调用 alert() 函数,它会在点击函数中用 mouseup 中断,因此点击函数中的 alert 是未触发。

关于javascript - 链接点击事件被父 mousedown 事件阻止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35303951/

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