How to Use ATTR and removeattr under jquery

Source: Internet
Author: User

Next, let's take a look at the usage of ATTR:
1. ATTR (name): gets the attribute value of the First Matching Element. This method can be used to conveniently obtain the value of an attribute from the First Matching Element. If the element does not have a property, undefined is returned. Here, the name is string. I will use an IMG element to demonstrate this usage: CopyCode The Code is as follows: //
// We can use ATTR to obtain the src attribute of the IMG element. The usage is as follows:
$ (Function (){
VaR imgsrc = $ ("IMG"). ATTR ('src ');
Alert (imgsrc); // displays a.gif
})

2. ATTR (Key, value): sets an attribute value for all matching elements. Key is the string attribute name, and value is the object attribute value. Usage Demonstration:Copy codeThe Code is as follows: //
// Use this method to set the src attribute of IMG
$ (Function (){
$ ("IMG"). ATTR ('src', 'a.gif '); //
})

3. ATTR (properties): This is the best way to set many attributes in batches among all matching elements. Note: To set the class attribute of an object, you must use 'classname' as the attribute name. Or you can directly use. addclass (class) and. removeclass (class ).Copy codeThe Code is as follows: //
$ (Function (){
$ ("IMG"). ATTR ({SRC: 'a.gif ', Title: 'This is a image', classname: 'filter '});
})
// Display as

4. ATTR (Key, FN): sets a calculated attribute value for all matching elements. Instead of providing a value, a function is provided. The value calculated by this function is used as the attribute value.Copy codeThe Code is as follows: //
$ (Function (){
$ ("IMG "). ATTR ('title', function () {return this. SRC}); // display
})

The above describes some usage of ATTR. The following is the usage of removeattr. Therefore, the meaning of removal is to delete an attribute from each matching element. Its specific usage is:Copy codeThe Code is as follows: //
$ (Function (){
$ ("IMG"). removeattr ('title'); // display
})

The above describes the basic usage of ATTR and removeattr, and some more complex applications will be encountered in actual work. This requires us to master the basic usage while, summarize and try other methods.

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.