JQuery. attr () and. Removeattr () Methods manipulating element Properties sample

Source: Internet
Author: User
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"
});
});
});

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.