如何从jQuery中的函数中排除特定标记?

时间:2015-02-24 18:53:19

标签: javascript jquery

我正在使用我检索到的一个功能,它可以在页面上平滑滚动。但是,该函数将其应用于以标签开头的所有标签。我需要该函数来排除类中存在的特定标记。

例如,我希望班级<a href="#">中的所有<a href="#somename">.someclass都可以从下面的函数中排除。

  $(function() {
    $('a[href*=#]:not([href=#])').click(function() {
      if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
        var target = $(this.hash);
        target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
        if (target.length) {
          $('html,body').animate({
            scrollTop: target.offset().top
          }, 1000);
          return false;
        }
      }
    });
  }); 

1 个答案:

答案 0 :(得分:2)

您可以使用not jquery功能吗?像:

$('a[href*=#]:not([href=#])').not(".someclass").click(new function() { /* */ })

或者,您也可以将其添加到selector not,例如:

 $('a[href*=#]:not([href=#],.someclass)').click(function() { /* */ })

http://jsfiddle.net/g252a4kp/