jQuery从“实时"切换应用程序.来“上"方法 [英] JQuery switching application from "live" to "on" method

查看:81
本文介绍了jQuery从“实时"切换应用程序.来“上"方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
jQuery 1.7-将live()转换为on()

Possible Duplicate:
jQuery 1.7 - Turning live() into on()

只需将我的代码从实时"切换为开启",某些事件就不再触发,这是一个例子,有人可以帮忙说一下这是怎么回事吗?在...之前,它使用实时"而不是开"方法可以100%正确地工作.

Just switching my code from "live" to "on" and some events just don't fire any more, here is an example, can anyone pls help out saying what's wrong with it? It WORKED 100% correct with "live" instead of "on" method before....

$('a#doBulkLink').on('click', function (e) {

    createLabelsWithDestinationFolders();

    $('label.moveDocDestinationFolder').on('click', function (e) {

        doSomeAjaxStuffWithLabels();
        e.stopImmediatePropagation();  

    });

    e.preventDefault();
});

谢谢.

推荐答案

您不能仅通过将函数名称更改为on()来将live()替换为on().签名也会随之改变.

You cannot replace live() with on() simply by changing the function name to on(); the signature changes as well.

$('selector').live('event', function () {

});

...变成....

... becomes ....

$(document).on('event', 'selector', function () {

});

以当前形式,您可以直接替换bind()(其中click()change()等是别名).这样,将处理程序直接绑定到元素,而不是将处理程序绑定到document并利用事件冒泡(live()所做的事情).

In it's current form, what you have is a direct replacement for bind() (of which click(), change() etc are aliases). As such, the handler is been bound directly to the element, rather than binding the handler to the document and taking advantage of event bubbling, which is what live() did.

这篇关于jQuery从“实时"切换应用程序.来“上"方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆