admin管理员组文章数量:1024054
How can I prevent an event from bubbling up to parent in javascript?
Eg.
<tr id="my_tr" onclick="javascript:my_tr_click();">
<td>
<a id="my_atag" href="javascript:void(0);" onclick="javascript:my_a_click();">Delete</a>
<td>
</tr>
When I click on "Delete" anchor tag, first the my_a_click() function gets called, and then the parent tr onclick function - my_tr_click() - gets called. This, I believe, is called event propagation.
How can I stop that my_tr_click() function from getting called when I click on the child anchor tag?
Please help me out..
Thanks
How can I prevent an event from bubbling up to parent in javascript?
Eg.
<tr id="my_tr" onclick="javascript:my_tr_click();">
<td>
<a id="my_atag" href="javascript:void(0);" onclick="javascript:my_a_click();">Delete</a>
<td>
</tr>
When I click on "Delete" anchor tag, first the my_a_click() function gets called, and then the parent tr onclick function - my_tr_click() - gets called. This, I believe, is called event propagation.
How can I stop that my_tr_click() function from getting called when I click on the child anchor tag?
Please help me out..
Thanks
Share Improve this question asked Apr 25, 2011 at 14:25 PrashantPrashant 5,46110 gold badges46 silver badges59 bronze badges2 Answers
Reset to default 4From quirks mode and also this answer this is most likely what you want to do to handle your inline onclick()
.
function my_a_click(e) {
e.cancelBubble = true;
if (e.stopPropagation) e.stopPropagation();
}
And adjust your markup:
<a id="my_atag" href="javascript:void(0);" onclick="var event = arguments[0] || window.event; my_a_click(event);">Delete</a>
Working example on jsfiddle
tested in chrome, firefox 4 and IE9.
With all that being said, if jQuery is an option it would make things a lot easier.
$("#my_atag").click(function(e){
e.stopPropagation();
});
and
<a id="my_atag" href="javascript:void(0);">Delete</a>
I think you may be interested in http://www.quirksmode/js/introevents.html
something.onclick = function (e) {
e.cancelBubble = true;
if (e.stopPropagation) e.stopPropagation();
}
How can I prevent an event from bubbling up to parent in javascript?
Eg.
<tr id="my_tr" onclick="javascript:my_tr_click();">
<td>
<a id="my_atag" href="javascript:void(0);" onclick="javascript:my_a_click();">Delete</a>
<td>
</tr>
When I click on "Delete" anchor tag, first the my_a_click() function gets called, and then the parent tr onclick function - my_tr_click() - gets called. This, I believe, is called event propagation.
How can I stop that my_tr_click() function from getting called when I click on the child anchor tag?
Please help me out..
Thanks
How can I prevent an event from bubbling up to parent in javascript?
Eg.
<tr id="my_tr" onclick="javascript:my_tr_click();">
<td>
<a id="my_atag" href="javascript:void(0);" onclick="javascript:my_a_click();">Delete</a>
<td>
</tr>
When I click on "Delete" anchor tag, first the my_a_click() function gets called, and then the parent tr onclick function - my_tr_click() - gets called. This, I believe, is called event propagation.
How can I stop that my_tr_click() function from getting called when I click on the child anchor tag?
Please help me out..
Thanks
Share Improve this question asked Apr 25, 2011 at 14:25 PrashantPrashant 5,46110 gold badges46 silver badges59 bronze badges2 Answers
Reset to default 4From quirks mode and also this answer this is most likely what you want to do to handle your inline onclick()
.
function my_a_click(e) {
e.cancelBubble = true;
if (e.stopPropagation) e.stopPropagation();
}
And adjust your markup:
<a id="my_atag" href="javascript:void(0);" onclick="var event = arguments[0] || window.event; my_a_click(event);">Delete</a>
Working example on jsfiddle
tested in chrome, firefox 4 and IE9.
With all that being said, if jQuery is an option it would make things a lot easier.
$("#my_atag").click(function(e){
e.stopPropagation();
});
and
<a id="my_atag" href="javascript:void(0);">Delete</a>
I think you may be interested in http://www.quirksmode/js/introevents.html
something.onclick = function (e) {
e.cancelBubble = true;
if (e.stopPropagation) e.stopPropagation();
}
本文标签: restrict event propagation in javascriptStack Overflow
版权声明:本文标题:restrict event propagation in javascript - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745607510a2158810.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论