点击按钮,段落没有变色,我无法理解这背后的原因。
<button onclick="changeBackGroundOfPs('#firstDiv');">Change backgrounds of p under a given element known by id</button>
<br>
<div id="firstDiv">
<p>First paragraph.</p>
<p>Second paragraph.</p>
</div>
function changeBackGroundOfPs(id ) {
var paragraphs = document.querySelectorAll(id p);
// Another way to iterate on all elements in a collection
for (var i = 0; i < paragraphs.length; i++ ) {
paragraphs[i].style.backgroundColor = "lightGreen";
}
}
为什么这可以在查询选择器(document.querySelectorAll(“#” + id + “ p”)中添加分号的情况下工作);.
<button onclick="changeBackGroundOfPs('firstDiv');">Change backgrounds of p under a given element known by id</button>
<br>
<div id="firstDiv">
<p>First paragraph.</p>
<p>Second paragraph.</p>
</div>
function changeBackGroundOfPs(id) {
var paragraphs = document.querySelectorAll("#" + id + " p");
// Another way to iterate on all elements in a collection
for (var i = 0; i < paragraphs.length; i++ ) {
paragraphs[i].style.backgroundColor = "lightGreen";
}
}
白板的微信
相关分类