我无法通过简单的功能更改按钮的颜色,颜色根本没有改变。
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script language="JavaScript">
function changeColor(){
document.getElementsByTagName('button').style.backgroundColor="green";
}
</script>
</head>
<body >
<form action="/action_page.php" method="get" name="form1">
<input type="text" id="campoDeFlores">
<button type="button" onclick="changeColor()" name="1">1</button>
<button type="button" name="2">2</button>
<button type="button" name="3">3</button>
</form>
</body>
</html>
为什么它不起作用?
原文由 user7128116 发布,翻译遵循 CC BY-SA 4.0 许可协议
document.getElementsByTagName
返回元素列表而不是单个元素。您需要使用Array.from
将其转换为数组,然后使用Array.map
遍历按钮