JavaScript operates class and style style code in detail

  • 2020-12-19 20:55:08
  • OfStack

Without further ado, I will post js code directly to you. The specific code is as follows:


<!doctype html>
<html>
<head>
<meta charset="utf-">
<title> Untitled document </title>
<style>
.fontSize
{
font-size:px;
}
.fontWeight
{
font-weight:bold;
}
</style>
</head>
<body>
<div id="div" class="fontSize fontWeight" style="color:red">div Examples of text </div>
<button onclick="changeStyle()">changeStyle()</button>
<script>
function changeStyle()
{
var div = document.getElementById("div");
//div.className = "fontSize";
//div.className += " fontWeight";
// Removing a single class="" style 
//div.className = div.className.replace(/fontSize/,"");
// Delete all class="" style 
//div.removeAttribute("class");
// delete style="" Single style in 
div.style.cssText = div.style.cssText.replace(/red/,"blue");
// delete style="" All styles in 
//div.style.cssText = "";
}
</script>
</body>
</html>

This is the complete explanation of class and style style code for JavaScript operations.


Related articles: