Attr is a property of jquery.
1--attr (name)
Gets the property value of the first matching element. This method makes it easy to get the value of an attribute from the first matching element. Returns undefined if the element does not have a corresponding attribute
For example:
Html:
<div class= "pz1z" id= "zzzz" >
<span>aaaa</span>
</div>
Js:
$ (document). Ready (function () {
var aa= $ ("div"). attr ("class");
Alert (AA)
});
The result of the print is the value of the Div Note Class Property pz1z
Note: This only gets the first matching element if the two div can only get the property value to the first one. can be filtered by selector (PZZ)
2--attr (properties) return value: JQuery
Properties Map
Sets an object in the form of a "name/value" to the property of all matching elements.
This is the best way to set many properties in bulk in all matching elements. Note that if you want to set the class property of an object, you must use ' className ' as the property name. Or you can use. addclass (Class) and. Removeclass (Class) directly.
Html:
Js:
$ ("img"). attr ({src: "Woqunimei", ID: "Buhao"});
Note: All attributes of the matching element will be changed if the filter condition is div then all div element attributes will change. At the same time its () has a pair of curly braces (PZZ),
3--attr (key, value) return value: JQuery
Sets the value of a property for all matching elements key value is of type string, which is the format of "", ""
Html:
Js:
$ ("Img#img1"). attr ("src", "Woqunimei");
var aa= $ ("Img#img1"). attr ("src");
Alert (AA)
Result
"Woqunimei" (attr (name) will only find the first matching element, all the conditions to be filtered)
Note: This can only change one attribute value of an element, and if you want to change multiple attributes, you may refer to the attr (properties) function. At the same time, it will also change the value of all element attributes (PZZ)
4--attr (key, function (index, attr)) return value JQuery
Key (Class) String function (index, attr) function
Change the value of an attribute for all matching elements, except that the second parameter is different from attr (key, value) and is no longer a value of type string, but a function
Example
HTML Code:
<input type= "Text"/>
<input type= "Text"/>
JQuery Code:
$ ("input[type= ' text ']"). attr ("Value", function () {
Return "I am a rookie"
});
$ ("img"). attr ("title", function () {return this.src});
Results:
(after the page loads, "I'm a rookie" appears in the text box)
Note: This will change all of the matching element property values, and will be affected if there are two text input boxes (PZZ)
5--REMOVEATTR (name) return value: JQuery
Remove an attribute from each of the matched elements,
Describe:
Delete the src attribute of an image in a document
HTML Code:
<input type= "text"/value= "I am a rookie" >
<input type= "text"/value= "I'm not a rookie" >
JQuery Code:
$ ("input"). Removeattr ("value");;
Results:
Two text box what word Read no;
Note: The deletion is a property value of all matching elements, attr the first letter capitalized. JQuery 1.7 or above supports the removal of the value of multiple properties at once ("input"). Removeattr ("class value");
Separating attributes with whitespace
The attr of JQuery