带有 jQ​​uery 委托的复杂选择器

发布于 2024-09-02 04:51:20 字数 411 浏览 0 评论 0原文

jQuery 1.4.2 中可与委托一起使用的选择器的复杂性是否有限制?

这对我有用:

   $('.activeTabsList').delegate('.activeTabsListItem', 'click', 
function() { 
   alert('here'); 
});

这不起作用:

    $('.activeTabsList').delegate('.activeTabsListItem:not(.selected)', 'click', 
function() { 
   alert('here'); 
});

正如您可能假设的那样,一次只有 1 个项目具有所选类别。当我单击其他选项卡时,我的委托处理程序仍然没有被触发。

Is there a restriction on the complexity of selectors that can be used with delegate in jQuery 1.4.2?

This works for me:

   $('.activeTabsList').delegate('.activeTabsListItem', 'click', 
function() { 
   alert('here'); 
});

This does not work:

    $('.activeTabsList').delegate('.activeTabsListItem:not(.selected)', 'click', 
function() { 
   alert('here'); 
});

As you can probably assume, there is only 1 item at a time that has the selected class. When I click the other tabs, my delegate handler is still not fired.

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

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

发布评论

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

评论(1

魂归处 2024-09-09 04:51:20

您拥有的代码可以工作,您可以在此处查看演示。确保您的选择器像您认为的那样匹配,这很可能是问题...。 delegate() 本身处理这种情况。

通常,这种情况是由于过度分配 selected 类而导致的:

$(".activeTabsListItem").click(function() {
  $(".activeTabsListItem").addClass("selected"); //should have been $(this)
});

The code you have works, you can see a demo here. Make sure that your selector matches like you think it does, this is most likely the issue....delegate() itself handles this case.

Usually this happens as a result of something like this, over-assigning the selected class:

$(".activeTabsListItem").click(function() {
  $(".activeTabsListItem").addClass("selected"); //should have been $(this)
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文