[英]How to select specific elements inside of another specific divs with query selector?
code is like this代码是这样的
<div class="linkPillar">
<a href="github"><img src="github.png" alt="GitHub"></a>
<a href="facebook"><img src="fb.png" alt="facebook"></a>
</div>
and i want to select all a's just inside of linkPillar without giving each of them an id.我想 select 所有的 a 都在 linkPillar 里面,而不给他们每个人一个 id。 is there any way to make this, thanks
有什么办法可以做吗,谢谢
You can use the query selector to find the elements:您可以使用查询选择器来查找元素:
console.log(document.querySelectorAll('.linkPillar a'));
<div class="linkPillar"> <a href="github"><img src="github.png" alt="GitHub"></a> <a href="facebook"><img src="fb.png" alt="facebook"></a> </div>
You can use getElementsByClassName()
method to return a collection of all elements in the document with the class name "linkPillar" and then use querySelectorAll
to fetch all of the a tag elements within that element.您可以使用
getElementsByClassName()
方法返回文档中所有元素的集合,其中 class 名称为“linkPillar”,然后使用querySelectorAll
获取该元素中的所有 a 标签元素。
var aTags = document.getElementsByClassName("linkPillar")[0].querySelectorAll("a"); console.log(aTags);
<div class="linkPillar"> <a href="github"><img src="github.png" alt="GitHub"></a> <a href="facebook"><img src="fb.png" alt="facebook"></a> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.