简体   繁体   English

jquery动态绑定.on()选择父母还是孩子?

[英]jquery dynamic binding .on() select parents or children?

For example, 例如,

$( "#dataTable tbody tr" ).on( "click", function() {
  alert( $( this ).text() );
});

$( "#dataTable tbody" ).on( "click", "tr", function() {
  alert( $( this ).text() );
});

.on() binds "tr" with click event handler. .on()用click事件处理程序绑定“tr”。 The first one select children and register click event handler directly. 第一个选择子项并直接注册click事件处理程序。 The second one select parent "tbody", and select children "tr" as an argument. 第二个选择父“tbody”,并选择子“tr”作为参数。

Are they both dynamic binding? 它们都是动态绑定吗? Do they have the same effect? 他们有同样的效果吗? What is the difference between these two? 这两者有什么区别?

No, only the second version is dynamic binding. 不,只有第二个版本是动态绑定。

It should be simple to understand. 它应该很容易理解。 When you have code like: 当你有像这样的代码:

$(selector).method(arguments);

jQuery finds all the elements that match the selector at the time you execute the code, and calls the method on them at that time. jQuery在您执行代码时查找与selector匹配的所有元素,并在此时调用它们上的method If you execute this code when the page is first loaded, it will only find the elements that match the selector in the initial document. 如果在首次加载页面时执行此代码,它将只找到与初始文档中的选择器匹配的元素。 If the tr elements are added dynamically, the first version will not find them, so it will not bind the click event to them. 如果动态添加tr元素,则第一个版本将找不到它们,因此它不会将click事件绑定到它们。

When you use .on() with a selector as the second argument, eg 当你使用带有选择器的.on()作为第二个参数时,例如

$(outerSelector).on(event, innerSelector, function...);

it works as follows. 它的工作原理如下。 It finds all the elements that match outerSelector , and binds a handler for the event to them; 它找到与outerSelector匹配的所有元素,并将事件的处理程序绑定到它们; these elements have to exist when you call .on() . 当你调用.on()时,这些元素必须存在。 When the event occurs, the handler checks whether the target matches innerSelector , and then it executes your callback function. 当事件发生时,处理程序检查目标是否与innerSelector匹配,然后它执行您的回调函数。 This is how it allows the event to work on dynamically-added elements. 这就是它允许事件处理动态添加元素的方式。

So the general rule is that outerSelector should refer to a static element in the document, while innerSelector refers to the dynamic elements. 所以一般规则是outerSelector应该引用文档中的静态元素,而innerSelector引用动态元素。

Neither is really dynamic , so to speak. 可以说,两者都不是真正的dynamic

The following will bind the onclick event to every #dataTable tbody tr on the page: 以下将onclick事件绑定到页面上的每个#dataTable tbody tr

$( "#dataTable tbody tr" ).on( "click", function() { /*event*/ });

The other will bind an onclick event to every #dataTable tbody on the page, and the event will only fire if one of its clicked descendents meets the selector tr (see Event Delegation ): 另一个将onclick事件绑定到页面上的每个#dataTable tbody只有当其中一个被点击的后代符合选择器tr ,该事件才会触发(请参阅事件委派 ):

$( "#dataTable tbody" ).on( "click", "tr", function() { /*event*/ });

The second can be considered dynamic because it simulates an onclick for the specified selector, whether or not any of those elements exist at the time of binding. 第二个可以被认为是动态的,因为它模拟指定选择器的onclick,无论绑定时是否存在任何这些元素。 But technically it's an event that's attached to #dataTable tbody . 但从技术上讲,这是一个附加到#dataTable tbody的事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM