首页 > 解决方案 > 更改文本颜色 javascript

问题描述

我想通过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>

标签: javascript

解决方案


尝试这个..

<!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>


推荐阅读