gpt4 book ai didi

javascript - 使用 AJAX 时解决与 jQuery live() 的冲突

转载 作者:行者123 更新时间:2023-11-30 13:33:39 25 4
gpt4 key购买 nike

我在属于名为“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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com