js改变或添加className

 

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>

 

posted @ 2018-09-03 11:43  暗恋桃埖源  阅读(14084)  评论(0编辑  收藏  举报