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: