The example in this article describes the usage of tag element attributes used by jQuery. Share it with everyone for your reference. The specific analysis is as follows:
The use of jQuery introduced here mainly includes how jQuery controls the page, including element attributes, css style, DOM model, form elements and event processing, etc.
Attributes of tag elements
Every tag in HTML has some attributes, and their tags present various states in the page, such as the following tag
This article further explains the page attribute control method from the perspective of jQuery.
1.each() traverses elements
Theeach(callback) method is mainly used to traverse the elements in the selector. It accepts a function as a parameter. This function accepts a parameter that refers to the serial number of the element. For marked attributes, you can use the each() method with the this keyword to get or set the attribute value corresponding to each element in the selector.
Use each() method to traverse all elements.
First paragraph
Second paragraph
Second paragraph
Second paragraph
Second paragraph
Second paragraph
Second paragraph
There are 7 P elements in the above code. First, use $("p") to obtain the set of all p elements in the page, and then use the each() method to traverse all pictures. Access the image through the this keyword, obtain the image's ID, and set the image's ID attribute. The function index of each() method is the serial number of the element.
2. Get the value of the attribute. attr(name) method
Except when iterating over the elements in the entire selector. Many times you need to get the value of the characteristics of an object. This can be easily achieved through the attr(name) method in jQuery. This method gets the first attribute value in the element set. If there is no match, return unfefined.
First paragraph
Second paragraph
Second paragraph
Second paragraph
Second paragraph
Second paragraph
Second paragraph
If you want to get the title attribute value of the second p, you can do it through the position selector.
attr() method can also set the value of the attribute. The general expression is
With the position selector, when the first button is clicked, the following two buttons are disabled at the same time.
Many times, we hope that the value of an attribute can change regularly based on different elements. At this time we can use attr(name,fn), the second parameter is a function. This function accepts one parameter, which is the serial number of the element, and the return value is a string.
Return content:
Sometimes for some elements, you want to set different attributes at the same time. If you use the above method, you need to set the attributes one by one. jQuery is very user-friendly, and attr() also provides a list setting attr(properties) method. Multiple properties can be set.
Execution result:
When setting the value of an element attribute, the attribute value can be deleted through the removeAttr(name) method. The element will then be restored to its default settings. For example, the following code makes all buttons undisabled.
I hope this article will be helpful to everyone’s jQuery programming.