开发者

Jquery click event assignment not working in Firefox

I'm assigning a click event to开发者_如何转开发 a bunch of anchors by class name, and it works in all browsers except Firefox, here is the JS:

var click_addthis = function(e, href) {
   if (!e) {
      var e = window.event;
   }
   e.cancelBubble = true;
   if (e.stopPropagation) e.stopPropagation();

   window.open(href, "Share It", null);

   return false;
}

$(document).ready(function() {
   $(".addthis_button_facebook").click(function() { click_addthis(event, this.href) });
   $(".addthis_button_twitter").click(function() { click_addthis(event, this.href) });
});

Am I missing something? Thanks


The problem area for Firefox is this section:

$(document).ready(function() {
   $(".addthis_button_facebook").click(function() { click_addthis(event, this.href) });
   $(".addthis_button_twitter").click(function() { click_addthis(event, this.href) });
});

You need to pass the event in from the handler so it's consistent, like this:

$(document).ready(function() {
  $(".addthis_button_facebook").click(function(e) { click_addthis(e, this.href) });
  $(".addthis_button_twitter").click(function(e) { click_addthis(e, this.href) });
});

You can also shorten it down to this since you're using the same function (return false stops propagation as well):

$(document).ready(function() {
  $(".addthis_button_facebook, .addthis_button_twitter").click(function() { 
    window.open(this.href, "Share It", null);
    return false;
  });
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜