jQuery停止子级触发父级事件 [英] jquery stop child triggering parent event
本文介绍了jQuery停止子级触发父级事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个div,我已将onclick
事件附加到该div.在这个div中,有一个带有链接的标签.当我单击链接时,也会触发div中的onclick
事件.我该如何禁用它,以便如果在div onclick
上单击链接不会被触发?
I have a div which I have attached an onclick
event to. in this div there is a tag with a link. When I click the link the onclick
event from the div is also triggered. How can i disable this so that if the link is clicked on the div onclick
is not fired?
脚本:
$(document).ready(function(){
$(".header").bind("click", function(){
$(this).children(".children").toggle();
});
})
html代码:
<div class="header">
<a href="link.html">some link</a>
<ul class="children">
<li>some list</li>
</ul>
</div>
推荐答案
执行以下操作:
$(document).ready(function(){
$(".header").click(function(){
$(this).children(".children").toggle();
});
$(".header a").click(function(e) {
e.stopPropagation();
});
});
如果您想详细了解.stopPropagation(),请点击此处.
这篇关于jQuery停止子级触发父级事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文