attr () method
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:
The code is as follows |
Copy Code |
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:
The code is as follows |
Copy Code |
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.
The code is as follows |
Copy Code |
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.
The code is as follows |
Copy Code |
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:
The code is as follows |
Copy Code |
$ (function () { $ ("img"). Removeattr (' title '); Show })
|
removeattr () method
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 Li's title attribute, so that's it:
The code is as follows |
Copy Code |
<script> $ ("ul Li:eq (1)"). Removeattr ("title"); </script> |
So simple, attr in fact is the original JS getattribute simplification, and removeattr is the removeattribute of the shorthand.
So is there a property similar to attr ()?
The Val () in jquery is similar,
$ (this). Val (); Gets the value of an element node, equivalent to $ (this). attr ("value");
$ (this). Val (value); Set the value value of an element node, equivalent to $ (this). attr ("value", value);
Now let's take a look at an example
The code is as follows |
Copy Code |
<title></title> <style type= "Text/css" > div{width:100px;margin:0px auto;border:1px solid red; </style> <script src= ". /js/jquery-1.5.1.min.js "type=" Text/javascript "></script> <script type= "Text/javascript" > $ (function () { $ ("#btnChange"). Click (function () { var $divs = $ ("div"); ----1. Example 1 $divs. EQ (0). attr ("myattr", "Ronaldo"); EQ returns the jquery object for the specified subscript (note: There are two Div DOM elements in $divs) $divs. EQ (1). attr ("MyAttr", "Ronaldo1"); Alert ($divs. attr ("myattr")); Read the MyAttr property, note that only the first element's property is read ----2. Example 2 use the JSON format to set attr $divs. EQ (0). attr ({"myattr": "Ronaldo", "Age": "19"}); $divs. EQ (1). attr ({"myattr": "Rivaldo", "Age": "29"}); Alert ("myattr:" + $divs. EQ (1). attr ("myattr") + ", Age:" + $divs. EQ (1). attr (' age ')); ----3. Example 3 removing attributes $divs. EQ (0). attr ({"myattr": "Ronaldo", "Age": "19"}); $divs. EQ (1). attr ({"myattr": "Rivaldo", "Age": "29"}); Alert ("myattr:" + $divs. EQ (1). attr ("myattr") + ", Age:" + $divs. EQ (1). attr (' age ')); $divs. EQ (1). Removeattr ("Age"); Alert ("myattr:" + $divs. EQ (1). attr ("myattr") + ", Age:" + $divs. EQ (1). attr (' age ')); }); }); </script> <body> <input id= "Btnchange" type= "button" value= "click Me"/> <div id= "Div1" > I am div1</div> <div id= "Div2" > I am div2</div> </body> |