我拥有的是
<script>
function myFunction()
{
document.getElementById("post").style.backgroundColor="#ECF6A4";
}
</script>
<button onclick="myFunction()">Change Post Color</button>
在页面中有多个 id="post"的 div,但是当我保存代码并尝试时,它只会更改页面上第一个 id="post"的 div 的背景,而不是任何其他人。
我尝试了 getElementsById 但没有成功,这就是我能想到的。
有什么简单的解决办法吗?
最佳答案
同一页面上具有相同 id 的多个 div 是无效标记,重复的 id 无效。
所以请使用类 因此
getElementsByClassName()
迭代它们
关于javascript - 如何使用 document.getElementById ("mydiv") 编辑具有相同 ID 的多个 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23226596/