How does jquery determine if an element has the specified style


It can be divided into the following two situations: 1. If the CSS is written as inline style, it can be judged by obtaining the value of the style attribute, as shown in the following example: Determine whether the div element with id divid has a font-size style:


<div id="divid" style="float:left; font-size:12px;"></div>
jquery The code is as follows:
jQuery("#divid").each(function(){
var fontSize = $(this).attr("style").indexOf("font-size");
if(fontSize != (-1)){alert(" The defined ");}
else{$(this).css({"float":"left","font-size":"12px"});}
});

Note: if there is only one div element with id divid, jquery’s each is executed only 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 whether the div element with id divid contains the class style divclass:


.divclass{
background-color: #F33;
}
<div id="divid" class="divclass"></div>

The jquery code is 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 clicked to switch the background color.