<html>
<head>
<style>
#div1
{
width:100px;
height:100px;
border:1px solid green;
}
.box
{
background-color:#FF0000;
}
</style>
<script>
function toRed()
{
var obj=document.getElementById("div1");
obj.className="box";//相当于在div添加class属性 仅class例外
}
</script>
</head>
<body>
<div id="div1">
</div>
<input type="button" onclick="toRed()" value="变红"/>
</body>
</html>
js添加class属性
最新推荐文章于 2024-11-28 07:24:01 发布