Copy codeThe Code is as follows: <! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Script src = "js/jquery-1.3.2.js"> </script>
<Script type = "text/javascript"> <! --
$ (Function (){
$ ("# AAttr1"). click (function (){
$ (": Text"). attr ("value", $ ("# divTest"). attr ("id "));
})
$ ("# AAttr2"). click (function (){
$ (": Text"). attr ("value", function (){
Return $ ("# divTest"). attr ("id ");
});
})
$ ("# AAttr3"). click (function (){
$ (": Text"). attr ({value: "test2 "});
})
$ ("# AAttr4"). click (function (){
$ ("# InputTest2"). removeAttr ("value ");
})
})
// --> </Script>
<Title> untitled document </title>
</Head>
<Body>
<Form>
<Input id = "inputTest1" type = "text"/>
<Input id = "inputTest2" type = "text" value = "test"/>
<Div id = "divTest"> 123 </div>
<A href = "#" id = "aAttr1"> display div id method 1 in text form </a> |
<A href = "#" id = "aAttr2"> method 2 for displaying the div id in the text form </a> |
<A href = "#" id = "aAttr3"> display test2 in text form </a> |
<A href = "#" id = "aAttr4"> remove the value attribute of inputTest2 </a> |
<Input type = "reset"/>
</Form>
</Body>
</Html>
1. element. attr (name)
Description: used to obtain the name attribute value of an element. In this example, $ ("# divTest"). attr ("id") can obtain the ID value of divTest.
2. element. attr (name, value)
Description: used to set the name attribute value of an element. For example, $ (": text") in the example "). attr ("value", $ ("# divTest "). attr ("id") assigns the ID value of divTest to the value of the text form.
Note: In the example (": text"), it is used to obtain the elements whose input form type is text. Other forms can be obtained in the same way, for example, <input type = "button"/> can be obtained with $ (": button"). The returned value is Array (Element), and $ (": input") is also available ") obtain all input elements .. You can also use element to obtain and set the text and value values of elements in JQuery. text ()/element. text (value), element. val ()/element.val(value.pdf, the method is the same as element.html (). If you have any questions, follow the instructions.
3. element. attr (name, function)
Description: it is used to set the name attribute value of an element. It is similar to the previous attribute value, but the value here can be written as a function, which is more flexible.
4. element. remove (name );
Description: Used to delete the name attribute of an element.