Simple use of jquery ATTR and removeattr

Source: Internet
Author: User

Usage 1: $ (Selector). ATTR (attribute name) is used to obtain the value of a specified attribute of a specified Element ($ (selector.Example:

There is such an HTML:

 

 

What should you do if you want to get the image address? This way: $ ("IMG "). ATTR ("src") is that simple. You can use alert or other forms to output the image address. what if I want to get the image description? This way: $ ("IMG "). ATTR ("ALT "). simple enough. it not only obtains the attributes of HTML, but also the attributes defined by you, such as the funny attribute in the previous example. You can try to obtain its value. note: If the property you want to obtain does not exist, jquery returns an undefined.

Usage 2: $ (selector). ATTR (attribute name, attribute value) sets an attribute value for all matching elements.

Suppose there are a bunch of such HTML in the page:

 

 

Let's write a sentence like this jqueryCode: $ ("IMG"). ATTR ("SRC", "http://t.douban.com/lpic/s00001510.jpg”), so that the above pile of meaningless IMG labels becomes:

 

 

It's easy to understand. if we want to set the image height, we only need $ ("IMG "). ATTR ("height", "300 ″). set the width as follows: $ ("IMG "). ATTR ("width", "500 ″). it seems that there is no problem, but it is too troublesome to set multiple attributes one by one, so let's look at the third usage.

Usage 3: $ (selector ). ATTR (MAP) is used to set multiple attribute values for a specified element. Let's take a look at what map means. it is actually a sequence like this:

{Attribute name 1: "attribute value 1", attribute name 2: "attribute value 2 ",... ... }

So I want to write the example in the second method as long:

$ ("IMG"). ATTR ({SRC: "http://t.douban.com/lpic/s%1510.jpg”, height:" 300 ", width:" 500 ″})

We learned how to get the property value and set the property value. How can we delete the property?

The keyword for deleting attributes in jquery is removeattr. Note that A is in upper case and how to use it:

The same is the HTML code in usage 1. If I want to delete the Image Height attribute, this is the case:

$ ("IMG"). removeattr ("height ");

Well, that's simple. ATTR is actually a simplified implementation of getattribute in native JS, while removeattr is short for removeattribute.

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.