`$(document).on(" click"," a"`和`$(" a"))之间的区别。click(`
任何人都可以告诉我这两个脚本之间有什么区别,我不是javascript / jquery专家。
Can Anybody tell me what will be the difference between these two script,Am not a javascript/jquery expert.
$(document).on("click", "a", function () {
i = $(this).data("value");
alert(i)
})
$("a").click(function () {
i = $(this).data("value");
alert(i)
});
$(文件)。 on(click,a,function(){
将绑定事件在 a
元素上,这些元素在绑定事件。这称为 事件委托 。
$(document).on("click", "a", function () {
will bind the event on the a
elements which are not present at the time of binding event. This is called as event delegation.
事件委托允许我们将单个事件监听器附加到父元素,该元素将为匹配选择器的所有后代触发,无论这些后代是否为现在存在或将来添加。
Event delegation allows us to attach a single event listener, to a parent element, that will fire for all descendants matching a selector, whether those descendants exist now or are added in the future.
而 $(a)。click(function(function) ){
只会将事件绑定到DOM中存在的 a
元素。
Whereas $("a").click(function () {
will bind events only to the a
elements which are present in DOM.