`

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

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

How to bind event to element after generated?
页面元素如何在生成后绑定事件?


Question:

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 .btn-delete-confirm 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 ?



Answer:

I would like to know which version of jQuery are you using?
According to the jQuery documentation:
.live() could have been used to bind events to future elements, but it has been deprecated.

.delegate() and .on() 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+.


引用


The .on() 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()



引用

.delegate( selector, eventType, handler ) - Returns: jQueryversion deprecated: 3.0

Description:
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" );
});



Examples:

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





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

Answer 2:

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 () {...});
        }
    });
});


comments:
- 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





-






分享到:
评论

相关推荐

    jQuery中bind(),live(),delegate

    jQuery中bind(),live(),delegate(),on()绑定事件方法实例详解共8页.pdf.zip

    jQuery中bind(),live(),delegate(),on()绑定事件方法实例详解

    本文实例分析了jQuery中bind(),live(),delegate(),on()绑定事件方法。分享给大家供大家参考,具体如下: 前言 因为项目中经常会有利用jquery操作dom元素的增删操作,所以会涉及到dom元素的绑定事件方式,简单的归纳...

    Jquery中.bind()、.live()、.delegate()和.on()之间的区别详解

    在我们日常开发中经常会使用到.bind()、.live()、.delegate()和.on(),有些同学会对这四者存在一些疑虑,所以下面这篇文章主要给大家介绍了关于Jquery中.bind()、.live()、.delegate()和.on()之间区别的相关资料,...

    jquery插件使用方法大全

    jQuery就是如此强大,你可以轻易地找到DOM中的任何元素,而这也是jQuery设计之初query的真实含义(查询)。 编辑本段Jquery对象  jquery提供了很多便利的函数,如each(fn),但是使用这些函数的前提是:你使用的对象...

    jQuery事件绑定on()、bind()与delegate() 方法详解

    之所以有这么多类型的绑定方法,是因为jQuery的版本更新的原因,如on()方法就是1.7以后出现的。 jQuery的事件绑定api页面上,提到live()方法已经过时,不建议使用。所以这里我们主要就看下以下三个方法:bind()、...

    jQuery的三种bind/One/Live/On事件绑定使用方法

    delegate()等方法来处理事件绑定,jQuery从性能优化以及方式统一方面考虑决定推出新的函数来统一事件绑定方法并且替换掉以前的方法。 on(events,[selector],[data],fn) events:一个或多个用空格分隔的事件类

    jQuery on()方法绑定动态元素的点击事件实例代码浅析

    之前就一直受这个问题的困扰,在jQuery1.7版本之后添加了on方法,之前就了解过,其优越性高于live(),bind(),delegate()等方法,在此之前项目中想用这个来测试结果发现,居然动态生成的标签点击了没反应,而live方法...

    【JavaScript源代码】JQuery绑定事件四种实现方法解析.docx

     jQuery中提供了四种事件监听方式,分别是bind、live、delegate、on,对应的解除监听的函数分别是unbind、die、undelegate、off。在开始看他们之前 一:bind(type,[data],function(eventObject)) bind是使用频率...

    jQuery中bind,live,delegate与one方法的用法及区别解析

    bind( )方法用于将一个处理程序附加到每个匹配元素的事件上并返回jQuery对象。 .bind(eventType[, evnetData], Handler(eventObject)) 其中,参数eventType是一个字符串,包含一个或多个javaScript事件类型,例如...

    jQuery中on()方法用法实例详解

    主要介绍了jQuery中on()方法用法,实例分析了on()方法的功能及各种常见的使用技巧,并对比分析了与bind(),live(),delegate()等方法的区别,需要的朋友可以参考下

    jquery新的绑定事件机制on方法的使用方法

    因为在此之前有 bind(), live(), delegate()等方法来处理事件绑定,jQuery从性能优化以及方式统一方面考虑决定推出新的函数来统一事件绑定方法并且替换掉以前的方法。 on(events,[selector],[data],fn) events:一个...

    jQuery中对未来的元素绑定事件用bind、live or on

    对未来的元素绑定事件不能用bind, 1、可以用live代替,但是要注意jquery的版本,根据官方文档,从1.7开始就不推荐live和delegate了,1.9里就去掉live了。 2、推荐用on代替(注:1.7及以上的版本才支持)。用法:on...

    jQuery最新1.4.4精简版+1.4中文手册

    (Bug) stopImmediatePropagation was not being honoured in live/delegate event handlers (#7217) (Bug) Fixed an issue where host and protocol were not compared case-insensitively when determining ...

    jQuery中绑定事件bind() on() live() one()的异同

    jQuery中绑定事件的四种方法,他们可以同时绑定一个或多个事件 bind()————————-版本号小于3.0(在Jquery3.0中已经移除,相应unbind()也移除) live()————————–版本号小于1.7(在Jquery1.7中已经...

    jQuery新的事件绑定机制on()示例应用

    因为在此之前有bind(), live(), delegate()等方法来处理事件绑定,jQuery从性能优化以及方式统一方面考虑决定推出新的函数来统一事件绑定方法并且替换掉以前的方法。 on(events,[selector],[data],fn) events:一个或...

    解决jQuery使用append添加的元素事件无效的问题

    jquery api官方的例子在新增的元素上添加事件 $[removed]("click",'#lyysb a'...自 jQuery 版本 1.7 起,on() 方法是 bind()、live() 和 delegate() 方法的新的替代品。 注意:使用 on() 方法添加的事件处理程序适用于

    关于jQuery新的事件绑定机制on()的使用技巧

    因为在此之前有bind(), live(), delegate()等方法来处理事件绑定,jQuery从性能优化以及方式统一方面考虑决定推出新的函数来统一事件绑定方法并且替换掉以前的方法。 on(events,[selector],[data],fn) events:一个或...

Global site tag (gtag.js) - Google Analytics