简体   繁体   English

如果有很多同一个类,如何使用JavaScript获取特定元素?

[英]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');

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.

相关问题 如何在JavaScript中以编程方式判断从哪个文件中将特定CSS类应用于HTML元素? - How can I programmatically tell, in JavaScript, from which file a particular CSS class is getting applied to an HTML element? 如何从 javascript 中相同的多个 div 类名中获取特定值? - How do I get the particular value from same multiple div class name in javascript? Javascript如何从elementFromPoint()返回的对象/元素中获取特定的子元素? - Javascript how can I get a particular child from an object/element returned by elementFromPoint()? 如何使用 javascript 添加元素 class? - How can I add an element class with javascript? HTML Helpers从模型属性生成ID。 如果许多元素具有相同的ID,如何使用JavaScript和CSS定位1个特定元素? - Html Helpers generates ID from model properties. How do I target 1 particular element with JavaScript and CSS if many elements have the same ID? 如何通过javascript获取特定坐标处元素的ID - How to get ID of an element at a particular coordinate by javascript 我怎样才能通过同一个班级的班级名称获取所有元素? - How i could get all element by class name of same class? 如何获取父元素的类? - How can I get the class of a parent element? 如何使用 JavaScript 获取元素的 HTML? - How can I get the HTML for an element with JavaScript? 我怎样才能在javascript中获取元素高度 - how can i get element height in javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM