JQuery Attribute-attr () method, jquery-attr
Definition and usage
The attr () method sets or returns the attribute value of the selected element. The working method varies depending on the parameters selected for the attr () method.
Return attribute value-return the attribute value of the selected element.
Syntax
$(selector).attr(attribute)
| Parameters |
Description |
| Selector |
Selected Element |
| Attribute |
Specifies the attribute of the selected element (selector) to be obtained. |
Example
<! DOCTYPE html>
Element a is the selected element, the selected attribute is the href attribute, so the final output is the href attribute http://www.cnblogs.com/
Set attribute/value-set the attribute and value of the selected element.
Syntax
$(selector).attr(attribute,value)
| Parameters |
Description |
| Attribute |
Specifies the name of the selector attribute. |
| Value |
Specifies the value of the selector attribute. |
Example
<! DOCTYPE html>
Element a is the selected element, the selected attribute is the href attribute, click the button, element a executes the attr () method, change the href attribute of Element a to the http://www.cnblogs.com/kay-refresh/p/5911193.html
Use the function to set the attribute/value-set the attribute and value of the selected element.
Syntax
$(selector).attr(attribute,function(index,oldvalue))
| Parameters |
Description |
| Attribute |
Specifies the name of the selector attribute. |
| Function (index, oldvalue) |
Specifies the function that returns the property value. This function can receive and use the index value and current attribute value of the selector. |
Example
<!DOCTYPE html>
The img element is the selected element and the selected attribute is width. click the button to obtain the width value of the img element. Then, the function is used to obtain the index value and oldvalue value of the img element and return the value.
Note: Attributes must be set in the selected element to be modified. In this example, the img element must contain the width attribute. the attr () method can be used effectively.
Set multiple attribute/value pairs-set more than one attribute and value for the selected element.
Syntax
$(selector).attr({attribute:value, attribute:value ...})
| Parameters |
Description |
| Attribute: value |
Specify one or more attribute/value pairs. |
Example
<!DOCTYPE html>
Note: {} brackets must be added when multiple attributes are specified at a time. For example: attr ({width: "100", height: "100 "})
The preceding four methods are commonly used in the attr () method. Due to poor language organization, language errors may occur in some places.
Not complete to be continued!