[英]How can I get a particular element with JavaScript if there are many with same class?
I have this code. 我有这个代码。 How can I get
'a'
of 'div1'
: 如何获得
'div1'
'a'
'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.
刚开始学习JavaScript。
This should do it: 应该这样做:
document.querySelector('#div1 > .a');
#
is the ID selector ; #
是ID选择器 ; #div
matches the element with id="div1"
#div
与id="div1"
匹配的元素 >
is the child selector >
是子选择器 .
is the class selector ; .a
matches any element with class="a"
.a
与class="a"
任何元素匹配 Together, this specifically selects the <div class="a">
element that is a child of <div id="div1">
. 这一起专门选择了
<div class="a">
元素,该元素是<div id="div1">
的子元素。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.