Usage One: $ (selector). attr (property name) Its function is to get the value of the specified property of the specified element (the $ (selector) portion). See Example:
Have such a section of HTML:
So what do you want to get to the address of the picture? So: $ ("img"). attr ("src") is so simple, you use alert or other forms of output to see the image of the address. So what do I need to get a picture description? So: $ ("img"). attr ("Alt"). Easy enough. Not only can it take the attributes of the HTML itself, but also the attributes you define, such as the funny attribute in the example above, try to get its value. Note: If you are going to get a property that does not exist, then jquery will return a undefined.
Usage Two: $ (selector). attr (property name, property value) Its function is to set a property value for all matching elements.
If there's a bunch of HTML in the page:
Let's write a sentence like this jquery code: $ ("img"). attr ("src", "yun_qi_img/s3791510.jpg") so the above pile of meaningless img tags becomes:
It's easy to understand. If we want to set the height of the picture, then just $ ("img"). attr ("height", "300′"). Then set the width to be like this: $ ("img"). attr ("width", "500′"). So it seems to be no problem, but when you want to set multiple properties, one of these is too cumbersome to write, so let's look at the third usage.
Usage Three: $ (selector). attr (map) It means to set multiple attribute values for the specified element, and we'll focus on what the map means. It is actually such a sequence:
{Property name one: "Property value One", property name Two: "Property value Two", ...}
So I'm going to implement the example in usage two as long as this is written:
$ ("img"). attr ({src: "yun_qi_img/s3791510.jpg", Height: "300′, Width:" 500′})
The above we learned to get the property value, set the property value, then how to delete the attribute?
The key word for deleting attributes in jquery is: removeattr Note that a is uppercase. See how it's used:
The same is the HTML code in the usage one, I want to delete the height attribute of the picture, so that's it:
$ ("img"). Removeattr ("height");
Well, it's that simple. Smart students must have seen, attr in fact is the original JS getattribute simplification implementation, and removeattr is RemoveAttribute's shorthand
, attr (name): Gets the property value of the first matching element. This method makes it easy to get the value of a property from the first matching element. Returns undefined if the element does not have a corresponding property. where name is string. Below I use an IMG element to illustrate this usage:
Copy code code as follows:
We can use attr to get the SRC attribute of the IMG element, as follows:
$ (function () {
var imgsrc = $ ("img"). attr (' src ');
alert (IMGSRC); Show A.gif
})
II, attr (key,value): Sets a property value for all matching elements. The key is a string property name, value is the object property, and is shown in the usage:
Copy code code as follows:
in HTML file
Use this method to set the SRC attribute of img
$ (function () {
$ ("img"). attr (' src ', ' a.gif '); The file shows
})
Attr (properties): This is the best way to set many properties in batches in all matching elements. Note that if you want to set the class attribute of an object, you must use ' classname ' as the property name. Or you can use the. addclass (Class) and. Removeclass (Class) directly.
Copy code code as follows:
in file
$ (function () {
$ ("img"). attr ({src: ' a.gif ', title: ' This is a image ', ClassName: ' Filter '});
})
Display as
attr (Key, FN): Sets a computed property value for all matching elements. Instead of providing a value, a function is provided that evaluates the value of the function as the property value.
Copy code code as follows:
File
$ (function () {
$ ("img"). attr (' title ', function () {return this.src}); Show
})
The above is a attr of some uses, the following is the use of removeattr remove so Minth meaning is removed, from each matching element to remove an attribute. Its specific use is:
Copy code code as follows:
$ (function () {
$ ("img"). Removeattr (' title '); Show
})
The above introduces the basic usage of attr and removeattr.