2015-02-24 60 views
1

我正在使用我检索的函数,该函数在页面上设置平滑滚动。然而,该功能将其应用于以标签开头的所有标签。我需要该函数来排除某个类中存在的特定标签。如何在jQuery中从函数中排除特定标记?

例如,我希望类别中的所有<a href="#"><a href="#somename">都被排除在下面的函数之外。

$(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; 
     } 
     } 
    }); 
    }); 
+2

'$('A [HREF]“)过滤器(功能( ('href')。indexOf('#')== 0;})' – 2015-02-24 19:22:30

+0

'$('。someclass a:not([href =“#”]) ,.someclass a:not(href =“#somename”)')'?这可能就是你想的。我不愿意将它作为答案发布,因为我不确定这是否真的是你想要的。这将从一个元素中检索所有的锚点标签,该类别的'.someclass'的'href'值不以'#'开始...... – War10ck 2015-02-24 19:28:01

+0

是的,肯定会使用filter()函数,你可以做一些困难的验证,在那里,并缓存匹配过滤器的jQuery对象可能是一个好主意。 – Burimi 2015-02-24 19:43:11

回答

2

可你只需要使用notjquery功能。像:

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

另外,您也可以包括在你selectornot,如:

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

http://jsfiddle.net/g252a4kp/

+0

恩,谢谢。我尝试了第一个没有用的选项。我会仔细检查并尝试您列出的替代选项。 – rpeg 2015-02-24 19:28:12

+0

第二个选项与第一个选项不一样。第二种选择属性不等于'#'的所有锚标签以及任何具有'.someclass'类的东西。逗号在选择器 – War10ck 2015-02-24 19:29:33

+0

之间充当'or'感谢您的澄清。 – rpeg 2015-02-24 19:39:47