The following 2 kinds of situations are divided:
1. If the CSS is written in inline style, you can judge by getting the value of the Style property, as shown in the following example:
Determine if the DIV element with ID divID has a font-size style:
Copy Code code as follows:
<div id= "divID" style= "float:left; font-size:12px; " ></div>
The jquery code is as follows:
JQuery ("#divid"). each (function () {
var fontsize = $ (this). attr ("style"). IndexOf ("Font-size");
if (fontsize!= ( -1)) {alert ("defined");}
else{$ (this). css ({"float": "Left", "Font-size": "12px"});
});
Note: If there is only one DIV element with ID divid, each of jquery is executed once.
2. If the CSS is written as a class style, it can be judged by getting the value of the class attribute, as shown in the following example:
Determine if the DIV element with ID divID contains a class style Divclass:
Copy Code code as follows:
. divclass{
Background-color: #F33;
}
<div id= "divID" class= "Divclass" ></div>
The jquery code is as follows:
Copy Code code as follows:
JQuery ("#divid"). Click (function () {
if (JQuery (this). attr ("Class"). IndexOf ("Divclass") >0) {
JQuery (This). Removeclass ("Divclass")
}else{
JQuery (This). addclass ("Divclass")
}
});
Note: The above code can be achieved by clicking the background color switch.