JQuery. attr () and. Removeattr () method manipulate element Properties sample _jquery

Source: Internet
Author: User
Today I'm going to share with you how to use jquery's. attr () and. Removeattr () methods to 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 allows us to easily read, add, change or delete any of the attributes in one (or more) element, and in jquery we can use the following methods:

1. attr (): the. attr () method in jquery allows you to easily read, add, or modify the attributes of an element (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 ()).

Now let's take a quick look at the syntax format of the attr () and. Removeattr () Methods:
. attr () method
The. 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

Syntax for reading properties
. attr (AttributeName);//attributename the property name of the element you want to get
This returns the string "string", and it is noteworthy that the. attr () method only gets the property value of the first matching element, and if you need the attribute value of each individual element, you need to rely on the jquery. each () or the. Map () method to implement it.

Syntax for setting the attribute value for an element
. attr (AttributeName, value);//Where AttributeName is the property name that the element needs to set, value is the corresponding element value
The above returns an object that is used primarily to set one or more properties for the specified element.

. Removeattr () method
. removeattr (AttributeName);//Where AttributeName is the name of the property to remove

The. 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, let's look at its specific application, first of all, we look at a simple HTML Demo:

This is a convenient way to make a picture flip, such as:
Html:
Copy Code code as follows:



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.