试了getElementsByName和getElementsByTagName都不行,只有getElementById可以,比如下面的例子
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script type="text/javascript">
function red() {
var colour=document.getElementsByTagName("div");
colour.style.color="red";
}
</script>
</head>
<body>
<div>php中文网</div>
<br/>
<input type="button" value='更改颜色' onclick="red()">
</body>
</html> Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
getElementsByTagName获取的是数组。数组不能直接用style属性来设置的,用colour[0].style.color="red";可以实现