我想通过下面的javascript来改变文本的颜色,但它不起作用,请建议如何解决这个问题,非常感谢
function myFunction() {
var x = document.getElementsByClassName("eee").getElementsByClassName("abc")
x.style.color = "red";
}<!DOCTYPE html>
<html>
<body>
<p id="demo" class="eee">
<p id="ccc" class="abc" style="color: green;">Click the button to change the color of this paragraph. </p>
</p>
<button onclick="myFunction()">Try it</button>
</body>
</html>
发布于 2021-04-13 15:57:44
试试这个..。
<!DOCTYPE html>
<html>
<body>
<div id="demo" class="eee">
<p id="ccc" class="abc" style="color: green;">Click the button to change the color of this paragraph.</p>
</div>
<button onclick="myFunction()">Try it</button>
<script>
function myFunction() {
var x = document.getElementsByClassName("abc")
for (let i=0; i<x.length; i++) {
x[i].style.color = "red";
}
}
</script>
</body>
</html>
https://stackoverflow.com/questions/67070676
复制相似问题