为什么 jQuery live() 不起作用?

发布于 2024-09-14 08:50:28 字数 334 浏览 2 评论 0原文

我有以下代码可以正常工作:

$('.ui-selectmenu-menu a').click(function() { alert('OK'); });

但是,如果我将其替换为:

$('.ui-selectmenu-menu a').live('click', function() { alert('OK'); });

它不起作用。

原因可能是什么?

(就我而言,可以在运行期间删除并再次添加 $('.ui-selectmenu-menu a') 元素。)

I have the following code which work fine:

$('.ui-selectmenu-menu a').click(function() { alert('OK'); });

However, if I replace it with:

$('.ui-selectmenu-menu a').live('click', function() { alert('OK'); });

it does not work.

What could be the reason for that ?

(In my case, $('.ui-selectmenu-menu a') elements could be removed and added again during the run.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

惜醉颜 2024-09-21 08:50:28

如果类发生更改,例如父级没有 class="ui-selectmenu-menu 那么选择器将不再匹配,请确保在发生任何事件后都不会发生这种情况。

与直接绑定不同对于元素,选择器不再匹配将停止 .live() 触发该元素事件的处理程序。

If the class changes e.g. the parent doesn't have class="ui-selectmenu-menu then the selector would no longer match, make sure this isn't happening after whatever events you have.

Unlike binding directly to the element, the selector no longer matching will stop a .live() handler from firing for that element's events.

春风十里 2024-09-21 08:50:28

尼克的回答很有道理。而且,请检查您是否有 jquery 版本 1.3 或更高版本。
.live() 是在 jquery 1.3 版本中添加的。

Nick's answer makes sense. But also, check that you have jquery version 1.3 or greater.
.live() was added in jquery version 1.3.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文