JavaScript操作class和style样式代码详解
废话不多说了,直接给大家贴js代码了,具体代码如下所示:
<!doctypehtml>
<html>
<head>
<metacharset="utf-">
<title>无标题文档</title>
<style>
.fontSize
{
font-size:px;
}
.fontWeight
{
font-weight:bold;
}
</style>
</head>
<body>
<divid="div"class="fontSizefontWeight"style="color:red">div实例文本</div>
<buttononclick="changeStyle()">changeStyle()</button>
<script>
functionchangeStyle()
{
vardiv=document.getElementById("div");
//div.className="fontSize";
//div.className+="fontWeight";
//删除单个class=""样式
//div.className=div.className.replace(/fontSize/,"");
//删除所有class=""样式
//div.removeAttribute("class");
//删除style=""中的单个样式
div.style.cssText=div.style.cssText.replace(/red/,"blue");
//删除style=""中的所有样式
//div.style.cssText="";
}
</script>
</body>
</html>
以上内容是针对JavaScript操作class和style样式代码详解的全部内容,希望对大家有所帮助。
热门推荐
10 读书祝福语女生短句简短
11 感谢同事新婚祝福语简短
12 感谢教官文案祝福语简短
13 员工聚会蛋糕祝福语简短
14 元宵发客户祝福语简短
15 给团队信心祝福语简短
16 医美的祝福语大全简短
17 异国姻缘的祝福语简短
18 兄妹拍照祝福语简短的话