Set css styles using functions in jquery and jquerycss styles

Source: Internet
Author: User

Set css styles using functions in jquery and jquerycss styles

Set css styles using functions in jquery

23:27:55

1. jquery syntax

$ (Selector).Css (Name,Function (index, value))

Name:Required. Specifies the name of the CSS attribute

Function (index, value ):

Specifies the function that returns the new value of the CSS attribute.

  • Index-optional. Returns the index position of the selector.
  • Value-optional. Accept the current value of the CSS attribute
2. function (index, value) Usage

It is equivalent to traversing all objects selected by the selector and setting the corresponding css attribute values. The index and value parameters are optional.

If you directly reference an existing function, as follows:

// Function colorStr () provides a random background color for the background color of the td tag $ ("td" ).css ("background-color", colorStr ()); // The above code is equivalent to assigning the same color value to all td background colors directly, just like the following code $ ("td" ).css ("background-color ", "green ");

In the above case, the td object will not be traversed, but all td objects will be set to the same color, for example, all become green

If you use function traversal, the background color of each td will be set to a different color. The Code is as follows:

// Use the function to call the colorStr function to traverse all td tags, so that the background color of each td is different $ ("td" ).css ("background-color", function () {return colorStr ()});

As follows:

 

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.