admin管理员组文章数量:1319476
there is some html code like this:
<ul>
<li id='root' class='tree-node'>
root
<ul>
<li class='tree-node' id='node1'>node1</li>
<li class='tree-node' id='node2'>node2</li>
<li class='tree-node' id='node3'>node3</li>
<li class='tree-node' id='node4'>node4
<ul>
<li class='tree-node' id='node4-1' >node4-1</li>
<li class='tree-node' id='node4-2' >node4-2</li>
</ul>
</li>
</ul>
</li>
<ul>
i want to bind click event to each tag which has tree-node class
and also binding click event to document
here is my code:
$(".tree-node").click(function(e){
console.log($(this).attr("id"));
});
$(document).click(function(e){
console.log("document clicked!");
});
Is there any alternative for stopPropagation() to stop event bubbling from node4-2 to its parents?
fiddle : /
there is some html code like this:
<ul>
<li id='root' class='tree-node'>
root
<ul>
<li class='tree-node' id='node1'>node1</li>
<li class='tree-node' id='node2'>node2</li>
<li class='tree-node' id='node3'>node3</li>
<li class='tree-node' id='node4'>node4
<ul>
<li class='tree-node' id='node4-1' >node4-1</li>
<li class='tree-node' id='node4-2' >node4-2</li>
</ul>
</li>
</ul>
</li>
<ul>
i want to bind click event to each tag which has tree-node class
and also binding click event to document
here is my code:
$(".tree-node").click(function(e){
console.log($(this).attr("id"));
});
$(document).click(function(e){
console.log("document clicked!");
});
Is there any alternative for stopPropagation() to stop event bubbling from node4-2 to its parents?
fiddle : http://jsfiddle/R6ySc/
Share Improve this question asked Oct 29, 2012 at 7:44 SiamiSiami 2433 silver badges10 bronze badges 2- Why you do not want to use stopPropagation? – Adil Commented Oct 29, 2012 at 7:46
- because i want to invoke document click event – Siami Commented Oct 29, 2012 at 7:47
1 Answer
Reset to default 7You could just use the document click event and handle everything from there.
$(document).click(function(e){
console.log("document clicked!");
var t = $(e.target);
if(t.attr("class") == "tree-node") {
console.log("tree node clicked");
}
});
Check this fiddle: http://jsfiddle/R6ySc/3/
as an alternative, you can also specify a selector in the onclick event for the document (check the jQuery documentation for this):
$(document).on("click", ".tree-node", function(e) {
e.stopPropagation();
console.log("tree node '" + $(this).attr("id") + "' clicked");
});
for reference, check the ments below
本文标签: javascripthow can i stop event bubbling without stopPropagation methodStack Overflow
版权声明:本文标题:javascript - how can i stop event bubbling without stopPropagation method - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742054761a2418224.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论