javascript - How to select specific elements inside of another specific divs with query selector? - Stack Overflow

code is like this<div class="linkPillar"><a href="github"><img src=

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. is there any way to make this, thanks

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. is there any way to make this, thanks

Share Improve this question edited May 30, 2021 at 10:45 biberman 5,7774 gold badges15 silver badges38 bronze badges asked May 30, 2021 at 10:40 ofaydnofaydn 431 silver badge4 bronze badges 2
  • You mean document.querySelectorAll('.linkPillar a');? – user15388024 Commented May 30, 2021 at 10:44
  • Does this answer your question? javascript selectors – biberman Commented May 30, 2021 at 10:51
Add a ment  | 

2 Answers 2

Reset to default 6

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.

var linkPillar = document.getElementsByClassName("linkPillar")[0]; 
var aTags = linkPillar.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>
<a href="twitter"><img src="twitter.png" alt="Twitter"></a>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742318691a4421363.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信