Home > Article > Web Front-end > jQuery Lesson 3 Code to Modify Element Attributes and Content_jquery
1. Operation attributes
The above describes how to filter out the required elements. After getting the element, you need to operate on it. A common requirement is to iterate through the obtained set of elements and perform an operation on each element. The function provided by jQuery is
each(iterator), where iterator is a function that accepts an integer as a parameter, indicating the number of elements. Let's look at a simple example.
Check the results using Firebug:
In the above example, the native JavaScript method is used to access attributes. jQuery provides a more general method to access attributes, attr:
attr(name), if name is a string, get the value of name of the attribute of the first element; if name is an object, the attributes of the object are copied to all elements of the packaging set as attributes of the element .
attr(name,value), when name is a string, set the value of attribute name to value. When value is a function, call this function for each element in the packaging set to set it The value of name is set to the return value of the function.
Look at a simple example, the html code still uses the above:
1 | |
2 |