javascript – 如何让JQuery事件为2个类工作?

<script>
$(function(){
    $('.logger').bind('click',function(){
        alert('we will log that the user shared something');
        return true; //keep going,pretend nothing happened.
    });

    $('.share').bind('click',function(){
        window.open('blah');
        return false; 
    });
});
</script>
<a href="#" class="share logger">Share this</a>

我希望能够触发记录器和共享功能.但是现在,只触发了共享功能.如果我评论共享功能,那么记录器功能正常.

我也尝试改变“类”,以便记录器在分享之前.

解决方法

当你返回false时;在停止事件传播的事件处理程序中,这就是未触发其他处理程序的原因.如果您只想停止认操作,请使用event.preventDefault().

这个article很好地解释了这个概念:

First off,return false is actually
doing three very separate things when
you call it:

  1. event.preventDefault();
  2. event.stopPropagation();
  3. Stops callback execution and returns
    immediately when called.

“Wait a
minute,” you cry! I only needed to
stop the default behavior! I don’t
need these other two items
… I think.

The only one of those three actions
needed to cancel the default behavior
is preventDefault(). Unless you mean to actually stop event propagation (bubbling),using return false will greatly increase the brittleness of your code

相关文章

页面搜索关键词突出 // 页面搜索关键词突出 $(function () {...
jQuery实时显示日期、时间 html: &lt;span id=&quot...
jQuery 添加水印 &lt;script src=&quot;../../../.....
中文:Sys.WebForms.PageRequestManagerParserErrorExceptio...
1. 用Response.Write方法 代码如下: Response.Write(&q...
Jquery实现按钮点击遮罩加载,处理完后恢复 思路: 1.点击按...