听 jQuery 中所有孩子的所有 onClick 事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16774160/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Listen to all onClick events of all children in jQuery
提问by Guest 86
I can listen to all mrow click events of a div, using sometihng like
我可以使用类似的东西来收听 div 的所有 mrow 点击事件
$('#mydiv').on('click', 'mrow', function() {
var moo = $(this).attr('id');
if (handlers[id]) {
event.stopPropagation();
handlers[id]();
}
Can I use a similar setup to listen on allclick events of allthe children of the div (Without setting up, separate listeners for every type)? Some part of the tree have handler functions and some don't, and I want these requests to propagate up within the div, until one that has a handler is found.
我可以使用类似的设置来侦听div的所有子项的所有点击事件吗(没有设置,每种类型的单独侦听器)?树的某些部分具有处理程序函数而有些则没有,我希望这些请求在 div 中向上传播,直到找到具有处理程序的请求。
回答by Mohammad Adil
$('#mydiv').on('click', '*', function() {
*
will bind to all element's within #mydiv
*
将绑定到所有元素的内 #mydiv
Demo -->
http://jsfiddle.net/Vjwqz/1/
回答by A. Wolff
For all descendants:
对于所有后代:
$('#mydiv').on('click', '*', function() {...});
For direct descendants: {what is called children in javascript}
对于直系后代:{javascript 中所谓的子代}
$('#mydiv').on('click', '> *', function() {...});
回答by Ronald
I think you can leave the selector
我想你可以离开选择器
$('#mydiv').on('click', function() {
selector Type: String A selector string to filter the descendants of the selected elements that trigger the event. If the selector is null or omitted, the event is always triggered when it reaches the selected element.
selector 类型:String 一个选择器字符串,用于过滤触发事件的选定元素的后代。如果选择器为 null 或省略,则始终在到达所选元素时触发事件。
回答by Logan
refer this http://api.jquery.com/all-selector/
参考这个http://api.jquery.com/all-selector/
try :
尝试 :
$('#mydiv').on('click', '#mydiv *', function() {
var moo = $(this).attr('id');
if (handlers[id]) {
event.stopPropagation();
handlers[id]();
}