The attr () method is used in query to get and set element properties, attr is an abbreviation for attribute (attribute), which is often used in jquery DOM operations attr ()
attr () has 4 expressions
- attr (incoming property name): Gets the value of the property
- Attr (property name, property value): Sets the value of the property
- Attr (property name, function value): Sets the function value of the property
- attr (attributes): Sets multiple attribute values for the specified element: {attribute name one: attribute value one, property name two: ' Property value two ', ...}
Removeattr () Delete method
. Removeattr (AttributeName): Removes an attribute (attribute) for each element in the matching element collection
Advantages:
attr and Removeattr are jquery. For attribute manipulation encapsulation, call the method directly on a JQuery object, it is easy to manipulate the property, and do not need to deliberately understand the browser's property name of the different problems
Note the issue:
There is a conceptual distinction in DOM: attribute and property are translated as "attributes", and "JS Advanced Programming" is translated into "attributes" and "attributes". Simply understood, attribute is the property of the DOM node's own
<!DOCTYPE HTML><HTML><Head> <Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8" /> <title></title> <style>input{Display:Block;margin:10px;padding:10px;background:#bbffaa;Border:1px solid #ccc; } </style> <Scriptsrc= "Http://libs.baidu.com/jquery/1.9.1/jquery.js"></Script></Head><Body> <H2>. attr () and. Removeattr ()</H2> <H3>. attr</H3> <form> <inputtype= "text"value= "Set Value" /> <inputtype= "text"value= "Get Value"/> <inputtype= "text"value= "Callback Stitching value" /> <inputtype= "text"value= "Delete Value" /> </form> <Scripttype= "Text/javascript"> //Locate the first input, set the value of the property value by attr $("Input:first"). attr ('value','. attr (AttributeName, value)') </Script> <Scripttype= "Text/javascript"> //find the second input, get the value of the property value by attr $("Input:eq (1)"). attr ('value') </Script> <Scripttype= "Text/javascript"> //find a third input by using a function to set the property //You can return the final desired property value based on other property values on the element //For example, we can associate a new value with an existing value: $("Input:eq (2)"). attr ('value',function(I, Val) {return 'through function Settings' +val}) </Script> <Scripttype= "Text/javascript"> //find fourth input by using removeattr to delete attributes $("Input:eq (3)"). Removeattr ('value') </Script></Body></HTML>
The attributes and styles of jquery. attr () and. Removeattr ()