我正在使用我檢索的函數,該函數在頁面上設置平滑滾動。然而,該功能將其應用於以標籤開頭的所有標籤。我需要該函數來排除某個類中存在的特定標籤。如何在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;
}
}
});
});
'$('A [HREF]「)過濾器(功能( ('href')。indexOf('#')== 0;})' – 2015-02-24 19:22:30
'$('。someclass a:not([href =「#」]) ,.someclass a:not(href =「#somename」)')'?這可能就是你想的。我不願意將它作爲答案發布,因爲我不確定這是否真的是你想要的。這將從一個元素中檢索所有的錨點標籤,該類別的'.someclass'的'href'值不以'#'開始...... – War10ck 2015-02-24 19:28:01
是的,肯定會使用filter()函數,你可以做一些困難的驗證,在那裏,並緩存匹配過濾器的jQuery對象可能是一個好主意。 – Burimi 2015-02-24 19:43:11