即时尝试设置td id =“red”字段的背景颜色,但它只设置第一个,最新的问题?我认为我将不得不设置不同的名称?Javascript背景表
这里是我的代码:
<table id="theTable">
<tr>
<td id= "red">0 - some txt</td>
<td>1 - some txt</td>
<td>2 - some txt</td>
</tr>
<tr>
<td id = "red">3 - some txt</td>
<td>4 - some txt</td>
<td>5 - some txt</td>
</tr>
<tr>
<td><button type="button" onclick="funcion('red')">Try it</button></td>
</tr>
</table>
<script>
function funcion (id) {
document.getElementById('red').style.backgroundColor = "red";
}
</script>
</html>
预先感谢您!
ID必须在文档范围内是唯一的。 'getElementById'应该只返回一个元素。 – Bergi
你需要使用类而不是ID来完成这样的工作。 – briosheje
有没有想过为什么他们称之为“getElementById”而不是“getElementsById”? – j08691