JQuery CSS style Control Learning notes

Source: Internet
Author: User

JQuery is not much said. This is a function library of JS and is quite common. Today we will summarize three jQuery operations for css.
((Selector).css ("name", "value ")
Detail (selector).css ({properties })
((Selector).css (name)
((Selector).css ("name", "value") sets a value for the CSS attribute given to all elements:
The code I just wrote is as follows: Copy codeThe Code is as follows: <Script type = "text/javascript" src = "http://ajax.microsoft.com/ajax/jquery/jquery-1.4.min.js"> </script>
<Script type = "text/javascript">
Var jq = jQuery. noConflict ();
Jq (document). ready (function (){
Jq ("# red"). click (function (){
Jq ("p" ).css ("background-color", "red ");
});
Jq ("# white"). click (function (){
Jq ("p" ).css ("background-color", "white ");
});
});
</Head> <body>
<P> when you click the following button, the background turns red </p>
<Button id = "red" type = "button"> red </button>
<Button id = "white" type = "button"> white </button>
</Body> Function css ({properties}) simultaneously sets values for a series of css attributes of all matched elements:
You will understand the code. Copy codeThe Code is as follows: <script type = "text/javascript">
Var jq = jQuery. noconflict ();
Jq (document). ready (function {
Jq ("# font_red"). click (function (){
Jq ("p.info" ).css ({"background-color": "red", "font-size" = "200% "});
});});
</Script>

The corresponding html can be written
<P class = "info"> when the background of the text changes to red, the text size will also increase by twice </p>
<Button id = "font_red" type = "button"> click here </button>
Let's take a look at the effect:

Let's take a look at the last css (name)
This is rarely used.
Look at the instance
Background (this).css ("background-color ");
Actually, the attribute value of "name" is returned, which is rarely used and will not be explained in detail.
JQuery Size must be listed separately.
Two dimensions are available: $ (selector). height (value) and $ (selector). width (value)
The two are used in the same way. An instance is listed below:Copy codeThe Code is as follows: <Script type = "text/javascript" src = "http://ajax.microsoft.com/ajax/jquery/jquery-1.4.min.js">
Var jq = jQuery. noConflict ();
Jq (document). ready (function (){
Jq ("# font"). click (function (){
Jq (". font_s"). height ("200px ");
Jq (". font_s"). width ("20px ");
});});
</Script>
</Head> <body>
<P class = "font_s"> the height of the p label box increases to 200px, and the width changes to 20px. </p>
<Button id = "font" type = "button"> click here to change </button>
</Body>

The following is the supplemental test code: Copy codeThe Code is as follows: <! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Title> untitled document </title>
<Style type = "text/css">
. Test1 {
Color: #0C9 ;}
. Test2 {
Color: red ;}
</Style>
<Script src = "js/jquery-1.3.2.js">
</Script>
<Script type = "text/javascript">
$ (Function (){
Var count = 2;
$ ("# AAdd"). click (function (){
$ ("# TestDIV"). addClass ("test1 ");
})
$ ("# ARemove"). click (function (){
$ ("# TestDIV"). removeClass ("test1 ");
})
$ ("# AToggle1"). click (function (){
$ ("# TestDIV"). toggleClass ("test2 ");
})
$ ("# AToggle2"). click (function (){
$ ("# TestDIV"). toggleClass ("test2", count ++ % 3 = 0 );
})
$ ("# ACSS1"). click (function (){
$ ("# TestDIV" ).css ({background: "red" }).css ("color", "white ");
})

})
</Script>
</Head>

<Body>
<Div id = "testDIV"> I will change the color. </div>
<A href = "#" id = "aAdd"> Add a style </a>
<A href = "#" id = "aRemove"> remove a style </a>
<A href = "#" id = "aToggle1"> change style </a>
<A href = "#" id = "aToggle2"> change the style several times </a>
<A href = "#" id = "aCSS1"> color change </a>
</Body>
</Html>

1. addClass (class)
Description: adds a specified style to the selected element set.
2. removeClass ([class])
Description: deletes a specified style from the selected element set.
3. toggleClass (class)
Description: replaces the selected Element Set style. Note: If the current element looks like a class, this style will be removed, which is equivalent to the combination of addClass and removeClass.
4. toggleClass (class, switch)
Description: similar to the previous method, the difference is that a switch is added to determine the style when the switch value is true.
5. css (styleName, value)/css (styleName)/css ({value })
Description: css (styleName, value) is used to set the styleName style attribute value of the current element to value, css (styleName) is used to obtain the styleName style attribute value of the current element, and css ({value }) for another method to set the style property value, refer to the example ^ for specific usage, because it is similar to element. the usage of attr is similar, so it is not described in detail here

Related Article

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.