【问题标题】:Can't get the :not selector to work on in my jQuery无法在我的 jQuery 中使用 :not 选择器
【发布时间】:2012-08-07 05:26:38
【问题描述】:

我是 HTML 和 jQuery 新手,需要一些帮助....

这是我的 HTML:

<div class="conteiner">
    <div class="calendar" style=""> </div>
    <div class="status"></div>
    <div class="avatar read"></div>
    <div class="text">
        <div class="activity mytivits-act"></div>
        <div class="comments"></div>
        <div class="text-conteiner">
            <h3>test remind</h3>
            <span class="grey"></span>
            <span class="send-reminder">Remind</span>
       </div>
    </div>
</div>

当用户点击提醒链接时,我试图从下面的 .live 函数中过滤掉点击(因为我有一个单独的函数来处理这种情况)

<span class="send-reminder">Remind</span>

使用下面的 jQuery 行,但它不起作用...知道如何解决它吗?

$('.text-conteiner:not(.send-reminder)').live('click', function(e){
    ...
}));

【问题讨论】:

    标签: jquery html click live


    【解决方案1】:

    我认为您可以停止事件传播。 :not() 在这里不起作用。

    $('.text-conteiner').live('click', function(e){
        e.stopPropagation();
        // your code
    });
    

    DEMO

    例如,你也可以这样做:

    $('.text-conteiner').live('click', function(e){
        alert('container');
    });
    $('.send-reminder').click(function(e) {
        e.stopPropagation();
        alert('remainder');
    })
    

    DEMO

    注意

    如果可能,那么使用 .on() 代替 .live() 使用 jQuery 1.7+ 进行委托事件处理。

    【讨论】:

    • 谢谢。我需要使用 .live() 因为我使用 ajax 渲染这些对象。
    • 您的第一个代码有帮助!非常感谢您的快速答复!
    【解决方案2】:

    send-reminder 范围内使用event.preventDefault()

    $('.text-conteiner span.send-reminder').on('click', function(e){
        e.preventDefault();
    }));
    

    【讨论】:

      猜你喜欢
      • 2012-09-17
      • 1970-01-01
      • 2017-02-12
      • 2016-09-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-04
      相关资源
      最近更新 更多