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