Divided into the following two situations:
1. If the css is written as an inline style, it can be judged by getting the value of the style attribute. The example is as follows:
Judge whether the div element with the ID divid has a font-size style:
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, Then jquery's each is only executed once.
2. If the css is written as a class style, it can be judged by getting the value of the class attribute. The example is as follows:
Judge whether the div element with the id divid contains the class style divclass:
.divclass{
background-color: #F33;
}
< div id="divid" class="divclass">
Note: The above code can switch the background color with one click.