2015-02-24 63 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