JQuery css () method to change an existing CSS style sheet

Source: Internet
Author: User

Using the CSS () method to change an existing CSS style sheet, the CSS () method has a variety of uses. One of these can accept two input parameters: style properties and style values, separated by commas. For example, to change the link color, you can write code like this:

1$ ("#61dh a"). css (' color ', ' #123456 ');

The 2//selector ' $ (' #61dh a ') ' represents all links under the element with Id ' #61dh '.

3//.css (' Color ', ' #123456 ') means to set the color to ' #123456 '

If you need to change multiple style properties, you can first define the property variable and then assign it directly to the CSS () method.

1var mycss = {

2background: ' #EEE ',

3width: ' 478px ',

4margin: ' 10px 0 0 ',

5padding: ' 5px 10px ',

6border: ' 1px solid #CCC '

7};

8$ ("#result"). CSS (DIVCSS);

The code above defines a CSS style property variable "Mycss", similar to creating an external CSS file, and then using the CSS () method to assign the attribute to the DIV with id ' #result '.

Additionally, the CSS () method provided by jquery can also view the CSS property values of an element.

For example, to see the color of the link, the code is as follows:

1$ ("#61dh a"). CSS ("color")

You will find that this is similar to the first example, but only one argument (style attribute) is passed here.

The last thing to introduce is how to set the link style after the mouse stroke (for example: color), you need to use the JQuery event class Method-hover (). It is worth noting that the hover () method needs to define two functions, one is the mouse is out of date, and the other is after the mouse is drawn. The specific methods are as follows:

1$ ("#61dh a"). css (' color ', ' #123456 ');

2$ ("#61dh a"). Hover (function () {

3$ (this). CSS (' color ', ' #999 ');

4},

5function () {

6$ (this). CSS (' color ', ' #123456 ');

7});

The two functions of the 8//hover () method are separated by commas

Perhaps smart you have noticed that this method is not concise, oh, in fact, the jquery hover () method is not used to change the CSS style. In practical use, it is recommended to use the Add/Remove CSS method to change the mouse across the link style.

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.