我最近制作了一个 jquery 代码,它应该隐藏一个元素,如果它的 href 等于另一个元素的,但我不能让它工作......
HTML
<div class="a">
<a href="1">
<img scr="a">
</a>
</div>
<div class="thumb">
<a href="1">
<img scr="a">
</a>
</div>
<div class="thumb b">
<a href="2">
<img scr="b">
</a>
</div>
CSS
.a{width:400px;height:100px;background-color:black;}
.thumb{width:400px;height:100px;background-color:green;}
.b{background-color:yellow;}
JQUERY
var mainhref = $(".a a").attr('href');
if($("a", this).attr('href') == mainhref ) {
$(".a").hide();
}
else {
$(".a").show
}
最佳答案
使用纯 javascript :D
let ar = document.getElementsByTagName('a'),
holdarray = [];
Array.from(ar, elem => {
if(holdarray.includes(elem.getAttribute('href')))
elem.parentNode.style.display = 'none'
else
holdarray.push(elem.getAttribute('href'))
})
.a { width:400px;height:100px;background-color:black; }
.thumb { width:400px;height:100px;background-color:green; }
.b { background-color:yellow; }
<div class="a" >
<a href="1"></a>
</div>
<div class="thumb">
<a href="1"></a>
</div>
<div class="thumb b">
<a href="2"></a>
</div>
关于javascript - 如果 href 等于,jquery 隐藏元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40257396/