js改变或添加className

时间:2024-10-21 20:37:45

js改变或添加className

 <style type="text/css">
.newDiv {
font-weight: bold;
}
</style>

js和html

<script>
function clk() {
var parent = document.getElementById("parent");
// parent.className = parent.className + " newDiv"; //添加classname 既html中的div 的class变为 class = "google newDiv" parent.className = " newDiv"; //改变classname 既html中的div 的class变为 class = "newDiv" }
</script> </head>
<body>
<input type="button" value="click" onclick="clk();"/>
<div id="parent" class="google">
<div >child</div>
</div> </body>