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. |