Working with CSS styles in jquery

Source: Internet
Author: User

1. Get and set styles

$ ("#tow"). attr ("class") Gets the class attribute with ID tow

$ ("#two"). attr ("Class", "Divclass") sets the class attribute with ID.

2. Append style

$ ("#two"). AddClass ("DivClass2") appends a style to an object with ID: divClass2

3. Remove Style

$ ("#two"). Removeclass ("Divclass") removes the style with the class name Divclass of the object with ID two.

$ (#two). Removeclass ("Divclass divClass2") removes multiple styles.

4. Switch class name

$ ("#two"). Toggleclass ("Anotherclass")//repeat Toggle Anotherclass style

5. Determine if a style is included

$ ("#two"). Hasclass ("another") ==$ ("#two"). Is (". another");

6. Get styles from CSS styles

$ ("div"). CSS ("color") sets the value of the Color property. $ (Element). CSS (Style)

Set a single style

$ ("div"). CSS ("Color", "red")

Set multiple styles

$ ("div"). css ({fontSize: "30px", Color: "Red"})

$ ("div"). CSS ("height", "30px") ==$ ("div"). Height ("30px")

$ ("div"). CSS ("width", "30px") ==$ ("div"). Height ("30px")

7.offset () method

Its function is to get the relative offset of the element in the current window, where the returned object contains two properties, that is, top and left.

Note: Valid only for visible elements.

var offset=$ ("div"). offset ();

var left=offset.left; Get left Offset

var top=offset.top; Get right Offset

8. Position () method

Its function is to get the relative offset of the element relative to the nearest position style property set to relative or absolute's grandfather node, as with offset (), and the object it returns also includes two properties i.e. top and left.

9. ScrollTop () method and ScrollLeft () method

$ ("div"). scrolltop (); Gets the distance from the top of the scroll bar of the element.

$ ("div"). ScrollLeft (); Gets the distance from the left of the scrollbar of the element.

10, the toggle and Slidetoggle methods in jquery can realize the display and hiding of an element. The difference is:

Toggle: The dynamic effect is from right to left. Lateral action.

Slidetoggle: Dynamic effect from bottom to top. The vertical action.

For example, to achieve a tree from bottom to top contraction of the dynamic effect, the use of Slidetoggle is OK.

$ (' input '). attr ("ReadOnly", True)//sets the INPUT element to ReadOnly
$ (' input '). attr ("ReadOnly", False)//Remove the ReadOnly attribute of the INPUT element
$ (' input '). attr ("Disabled", true)//sets the INPUT element to disabled
$ (' input '). attr ("disabled", false)//Remove the disabled attribute of the INPUT element

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.