我的jquery代码与我的链接冲突



我的jquery代码与我的链接冲突,导致它们无法在新窗口中打开"target=_blank"?

 onReady:function(){
          $('a').click(function(event) {
            event.preventDefault();
            CookieControl.setConsent(true);
            CookieControl.closeAndHide();
            window.location = $(this).attr('href');
          });      
 },

受影响的链接<a href="www.google.com" target="_blank">Link</a>

您的回调做的第一件事就是取消事件!

 event.preventDefault();

与其将事件绑定到所有<a>标签,不如将类添加到要定位的一个或多个链接中,如下所示:

<a class="lnkSpecial" href="http://www.google.com" target="_blank">Link</a>

然后将事件绑定到该类:

  $('.lnkSpecial').click(function(event) {
      event.preventDefault();
      CookieControl.setConsent(true);
      CookieControl.closeAndHide();
      window.location = $(this).attr('href');
  });

这将阻止仅对与类的链接或链接执行默认操作。

最新更新