Reb*_*ers 2 javascript element object setattribute
我是JavaScript的初学者,我无法理解为什么在这个简单的代码中,出现在控制台color.setAttribute不是一个函数
<style>
.red {color:red;}
.blue { color: blue;}
</style>
</head>
<body>
<p class="red">Hello World</p>
<script>
var color = document.getElementsByClassName("red");
color.setAttribute("class","blue");
</script>Run Code Online (Sandbox Code Playgroud)
在此先感谢,如果我的问题很愚蠢,那就很抱歉.
document.getElementsByClassName("red")返回一个像对象一样的dom对象.所以你应该写下面的内容.
var color=document.getElementsByClassName("red")[0];
color.setAttribute("class","blue");
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
142 次 |
| 最近记录: |