I have this code. How can I get 'a'
of 'div1'
:
<div id="div1">
<div class="a"></div>
<div class="b"></div>
</div>
<div id="div2">
<div class="a"></div>
<div class="b"></div>
</div>
Thanks for help. Just started learning JavaScript.
This should do it:
document.querySelector('#div1 > .a');
#
is the ID selector ; #div
matches the element with id="div1"
>
is the child selector .
is the class selector ; .a
matches any element with class="a"
Together, this specifically selects the <div class="a">
element that is a child of <div id="div1">
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.