JavaScript操作class和style样式代码详解

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

JavaScript操作class和style样式代码详解

cag2050   2020-05-15 我要评论

废话不多说了,直接给大家贴js代码了,具体代码如下所示:

<!doctype html>
<html>
<head>
<meta charset="utf-">
<title>无标题文档</title>
<style>
.fontSize
{
font-size:px;
}
.fontWeight
{
font-weight:bold;
}
</style>
</head>
<body>
<div id="div" class="fontSize fontWeight" style="color:red">div实例文本</div>
<button onclick="changeStyle()">changeStyle()</button>
<script>
function changeStyle()
{
var div = document.getElementById("div");
/https://img.qb5200.com/download-x/div.className = "fontSize";
/https://img.qb5200.com/download-x/div.className += " fontWeight";
//删除单个class=""样式
/https://img.qb5200.com/download-x/div.className = div.className.replace(/fontSize/,"");
//删除所有class=""样式
/https://img.qb5200.com/download-x/div.removeAttribute("class");
//删除style=""中的单个样式
div.style.cssText = div.style.cssText.replace(/red/,"blue");
//删除style=""中的所有样式
/https://img.qb5200.com/download-x/div.style.cssText = "";
}
</script>
</body>
</html>

以上内容是针对JavaScript操作class和style样式代码详解的全部内容,希望对大家有所帮助。

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们