jQuery .on不起作用,但.live起作用 [英] jQuery .on does not work but .live does
问题描述
由于从1.7版开始不推荐使用live()方法,因此我开始遍历源代码并将所有活动事件处理程序都转换为on().我的印象是,更改将非常简单,并且一切将像以前一样工作.但是,我遇到了一些行为不正常的代码.
Since the live() method is deprecated as of version 1.7, I started going through my source and converting all of my live event handlers over to on(). I was under the impression that the change would be simple and everything would work as it had before; however, I ran into some code that doesn't behave as it should.
我有以下jQuery选择来绑定表标签的click事件...
I have the following jQuery select to bind the click event of a table tag...
$('table.accordion-header').live("click", function ($e) {
// gobs of code
}
...,它可以正常工作(即,即使在页面上发生异步回发之后,也会引发我的表标签点击事件).但是,如果我将代码更改为以下内容
... and it works just fine (ie - my table tag click event is raised even after asynchronous postbacks on the page occur). But if I change the code to the following
$('table.accordion-header').on("click", function ($e) {
// gobs of code
}
然后,在页面上发生任何异步回发后,不再引发click事件.请注意-click事件确实适用于所有异步回发,但此后将不再起作用.那我在这里想念什么?
then the click event is no longer raised after any asynchronous postbacks on the page occur. Please note - the click event does work up to any asynchronous postbacks, but afterwards it no longer works. So what am I missing here?
推荐答案
等同于
$('table.accordion-header').live("click", function ($e) {
// gobs of code
} );
是
$(document).on("click", 'table.accordion-header', function ($e) {
// gobs of code
} );
这篇关于jQuery .on不起作用,但.live起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!