How jquery determines whether an element has a specified style _jquery

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.