好吧,我以前涉足过 JavaScript,但我写过的最有用的东西是 CSS 样式切换器。所以我对此有点陌生。假设我有这样的 HTML 代码:
<div class="first">
<div id="bar">
Hello world!
</div>
</div>
不工作:document.getElementsByClassName("first").getElementById("bar")[0];
最佳答案
您可能想使用document.querySelector
.
这样您就可以使用 CSS 选择器并执行以下操作:
var bar_element = document.querySelector(".first #bar");
关于javascript - 通过 ID 和类获取元素内的元素 - JavaScript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49251004/