This article is a detailed description of how to use jquery's. attr () and. Removeattr () methods to read, add, modify, delete the attributes of the element, not friends can refer to ha, hope to help you all
To share with you today how to use jquery. attr () and. Removeattr () methods read, add, modify, and delete the attributes of an element. Everyone in the usual web page production has encountered how to dynamically get elements of attributes and property values, or dynamically modify the elements of a (certain) property values. Then jquery can make it easy for us to read, add, change, or delete any of the attributes in one (or more) element, in jquery we can use the following method:
1 attr (): the. attr () method in jquery allows you to easily read, Add or modify the attributes of an element (for details reference. attr ());
2. Removeattr (): the. Removeattr () method in jquery is used primarily to delete one (or more) attributes of an element (for details reference. REMOVEATTR ()).
Let's take a quick look at the syntax format of the attr () and. Removeattr () Methods:
. attr () method
. attr () method has two functions, the first is to read the attribute value of the element, and the second is to modify the attribute value of the element
Read the syntax of the property
. attr (AttributeName);//attributename the property name of the element that you want to get
The string is returned above, it is noteworthy. attr () method to get the property value of the first matching element only, if you need the attribute value of each individual element, you need to rely on the jquery. each () or. Map () method.
is the syntax
. attr (AttributeName, value) of the property value for the element, or the property name to which the element needs to be set, and value is the corresponding element value
which returns an object, Used primarily to set one or more properties for a specified element.
. Removeattr () method
. removeattr (AttributeName);//Where AttributeName is the name of the property to be removed
. removeattr () Method uses the RemoveAttribute () function in native JavaScript, but its advantage is that it can be invoked directly by jquery object access.
above we have a simple understanding of the. attr () and. Removeattr () method syntax, below weTake a look at its specific application, first of all, let's look at a simple HTML Demo:
This method is used to make a picture flip is very convenient, such as:
HTML:
Copy Code code as follows:
<img src= "images/a.jpg" alt= "header" Width= "" "height=" "" Class= "img"/>
Js:
Copy Code code as follows:
$ ("document"). Ready (function () {
$ (". img"). Hover (function () {
$ (this). attr ({
"src": "Images/b.jpg",
"Alt": "Change the page"
})
},function () {
$ (this). attr ({
"src": "Images/a.jpg",
"Alt": "Header"
});
});
});