我有许多元素,它们的href值分别是#-5、#slider-6、#slider-7等等。还有一些包含href值的其他元素,其中包含#,但模式不同。我想编写一个选择器逻辑,它只针对第二类元素,即那些在其href中包含了#的元素,而不是#滑块的组合元素。我就是这么写的:
$('a[href*=#]:not([href=#], [href=#slider])')但是,这个逻辑似乎不符合#滑块排除逻辑。
我需要的是选择所有包含#的标记,但不包括以下内容:
发布于 2017-02-16 15:23:16
您可以使用选择器:$('a[href*="#"]:not([href="#"], [href^="#slider"])')
发布于 2017-02-16 15:19:59
与其尝试实现一些奇怪的CSS选择器,不如使用.filter()函数来筛选元素。
$(document).ready(function() {
$('a')
.filter(function() {
return $(this).attr('href').indexOf('#slider-') == 0;
})
.addClass('selected');
})a {
text-decoration: none;
color: inherit;
}
.selected {
border-bottom: 1px solid red;
}<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href="#slider-1">Slider-1</a><br/>
<a href="#slider-2">Slider-2</a><br/>
<a href="#slider">Slider</a><br/>
<a href="#">#</a>
https://stackoverflow.com/questions/42277729
复制相似问题