javascript - 更改文本颜色 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>
解决方案
尝试这个..
<!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>
推荐阅读
- python - 创建将输入 csv 文件加载到应用程序的模块 - dijkstra 算法
- javascript - 使用 Axios 解析包装类型数组
- swift - 如何使用 Timer 在 GameViewController 中从一个场景过渡到另一个场景?
- c# - 动态 DataGrid 未显示表错误
- python - 如何创建可以使用作为子进程运行的终端修改的 PyQt5 UI
- rust - 当对 None 值调用 unwrap 时,try 块中的代码仍然会出现恐慌
- java - 非法 base64url 字符:“”从令牌 Java JWT Spring Boot 获取声明/解码时
- java - 带参数和不带参数的 toString()
- reactjs - AWS Cognito 与 django 休息框架反应 js?
- vue.js - Vuejs 转换无法显示模态