[英]Trying to hide an element with id
I am trying to know if this is possible: 我想知道这是否可能:
I have an element with an id but I'd like to know if I can hidde other elements which they do not have that id 我有一个带有ID的元素,但我想知道是否可以隐藏其他没有该ID的元素
I mean: 我的意思是:
<div id="tag1">
Hi 1
</div>
<div id="tag2">
Hi 2
</div>
.... to N elements ....到N个元素
<div id="tagN">
Hi N
</div>
$(document).ready(function(){
?????
});
How can I show tag1 and hide tag2 to tagN elements? 如何显示tag1和将tag2隐藏到tagN元素?
Thanks 谢谢
You can use starts with selector: 您可以使用选择器开头 :
$('div[id^="tag"]').not("#tag1").hide();
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="tag1"> Hi 1 </div> <div id="tag2"> Hi 2 </div> <div id="tag3"> Hi 3 </div>
But your code will have more performance if you add a class for all elements instead of selecting by attribute, like I did above. 但是,如果为所有元素添加一个类而不是像上面那样按属性选择,则代码将具有更高的性能。 Eg: 例如:
$("div.tag").not("#tag1").hide();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.