jquery之live(), delegate(), on() 方法

通过下面的一个实例理解 jQuery 的 live(), delegate(), on() 方法

[b][url=http://stackoverflow.com/questions/40818968/how-to-bind-event-to-element-after-generateing][size=medium]How to bind event to element after generated?[/size][/url] [/b]
[b]页面元素如何在生成后绑定事件?[/b]


[b]Question:[/b]

I have this script :


$(window).load(function () {
$(document).on('click', '.btn-delete-confirm', function () {...});
});


and I have this element :


<div id="attachments"></div>


and I have another script to load some html :


$(document).on('click', '.nav-tabs li a[href="#attach"]', function () {

$.ajax({
url: loadAttachmentsURL,
data: { equipmentId: equipmentId },
success: function (data) {
$("#attachments").html(data);
}
});

});

in my result from ajax I have some button that have [color=blue].btn-delete-confirm[/color] class
but when clicked on them nothing happen .

the sample of result like this :


<td><a data-id="73b2db39-199c-845c-8807-6c6164d2d97d"
data-url="/Admin/EquipmentAttachment/Delete"
class="btn-delete-confirm btn">Delete</a></td>


how can I resolve this ?


[b]Answer:[/b]

I would like to know which version of jQuery are you using?
According to the jQuery documentation:
[url=http://api.jquery.com/live].live()[/url] could have been used to bind events to future elements, but it has been deprecated.

[url=http://api.jquery.com/delegate].delegate()[/url] and [url=http://api.jquery.com/on].on()[/url] can be used for this
purpose now. I can see that the code you wrote uses .on().
This should work fine unless you are not using version 1.7+.


[quote][color=olive]

The [url=http://api.jquery.com/on].on()[/url] method attaches event handlers to the currently selected set of elements in the jQuery object.

As of jQuery 1.7, the .on() method provides all functionality required for attaching event handlers.

- For help in converting from older jQuery event methods, see .bind(), .delegate(), and .live().
- To remove events bound with .on(), see .off().
- To attach an event that runs only once and then removes itself, see .one()

[/color][/quote]

[quote]
[b].delegate( selector, eventType, handler )[/b] - Returns: jQueryversion deprecated: 3.0

[b]Description:[/b]
Attach a handler to one or more events for all elements that match
the selector, now or in the future, based on a specific set of root elements.


// jQuery 1.4.3+
$( elements ).delegate( selector, events, data, handler );
// jQuery 1.7+
$( elements ).on( events, selector, data, handler );



$( "table" ).delegate( "td", "click", function() {
$( this ).toggleClass( "chosen" );
});



$( "table" ).on( "click", "td", function() {
$( this ).toggleClass( "chosen" );
});



[b]Examples:[/b]


$( "body" ).delegate( "p", "click", function() {
$( this ).after( "<p>Another paragraph!</p>" );
});



[/quote]

------------------------------------------------------------------

[b]Answer 2:[/b]

You are trying to add an eventlistener to something that isnt there yet.
This will result in an error, and the event wont fire again.

So try to add the listener AFTER the ajax import.


$(document).on('click', '.nav-tabs li a[href="#attach"]', function () {

$.ajax({
url: loadAttachmentsURL,
data: { equipmentId: equipmentId },
success: function (data) {
$('#attachments').html(data);
$('.btn-delete-confirm').on('click', function () {...});
}
});
});


[b]comments:[/b]
- I dont want to repeat the same code .
- your answer destroys the elegance of delegation of events for dynamic elements.


-
引用请注明:
原文出处:http://lixh1986.iteye.com/blog/2341345


-
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值