Home > Web Front-end > JS Tutorial > body text

How jquery determines whether an element has a specified style_jquery

WBOY
Release: 2016-05-16 17:17:23
Original
1476 people have browsed it

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:

Copy code The code is as follows:


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:
Copy code The code is as follows:

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


The jquery code is as follows:
Copy code The 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 switch the background color with one click.
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!