我在属于名为“go”的类的页面上有按钮。我使用以下代码:
$(".go").live('click', this.handleAction);
我还有 ajax 事件会加载另一段 HTML 和 JS 来替换上面的按钮和内容,但它也有一段代码可能会发生冲突:
$(".go").live('click', this.handleAnotherAction);
我知道显而易见的答案是将类名更改为“go”以外的其他名称。但是还有其他方法可以在不更改新的 AJAX 插入元素的类名的情况下执行此操作吗?也许有一种方法可以重置 live() 绑定(bind)?
最佳答案
如果要从元素中移除live
绑定(bind),您需要使用die
解除绑定(bind)功能。请注意,die
的选择器必须与用于live
的选择器完全匹配:
// create live bindings:
$(".go").live('click', this.handleAction);
// remove live bindings:
$(".go").die('click');
// you can also specify the method you want to unbind, in case there are multiple live bindings:
$(".go").die('click', this.handleAction);
// rebind to a new live
$(".go").live('click', this.handleAnotherAction);
关于javascript - 使用 AJAX 时解决与 jQuery live() 的冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5768587/