- . Html ()Use HTML tags for reading and modifying elements
- . Text ()The plain text content used to read or modify elements.
- . Val ()Used to read or modify the value of a form element.
Functional Comparison of the three methods
- The three types of content .html (,.text(),.val()..) are used to read and select elements. Only the content .html () is used to read the HTML content of elements (including its HTML tags ),. text () is used to read the plain text content of an element, including its child element ,. val () is the "value" value used to read form elements. Except () is the same. If it is applied to multiple elements, it can only read the "value" value of the first form element,. text () is different from them, if. when text () is applied to multiple elements, the text content of all selected elements is read.
- . HTML (htmlstring ),. text (textstring) and. val (value) is used to replace the content of the selected element. If the three methods are applied to multiple elements at the same time, the content of all selected elements will be replaced.
- . Html (),. Text (),. Val () can use the return value of the callback function to dynamically change the content of multiple elements.
Example: <Div id = "Div">
<P> here is a paragram </P>
<Div> here is a big Div </div>
</Div>
Function showval ()
{
VaR DIV = $ ("# Div ");
Alert ("showval ():" + Div. Val ());
Blank
}
Function showhtml ()
{
VaR DIV = $ ("# Div ");
Alert ("showhtml ():" nvidiv.html ());
Shown:
<P> here is a paragram </P>
<Div> here is a big Div </div>
}
Function showtext ()
{Var Div =$ ("# Div ");
Alert ("showtext ():" + Div. Text ());
Shown:
Here is a paragram
Here is a big Div
}
Conclusion: Val () --- is generally used in input, instead of other elements, to obtain the input or select values.
HTML () and text () can be used on multiple elements, but HTML () is equivalent to returning based on the source code, that is, the returned content will contain various tags, while text () the returned content is the content between various tags.
There are two methods in the HTML attribute: one with a parameter and the other without a parameter.
1. No parameter HTML (): obtains the HTML content of the First Matching Element. This function cannot be used in XML documents. But it can be used in XHTML documents and returns a string
Example: HTML page code: <div> <p> Hello </P> </div> Jquery code: $ ("Div" ).html (); Result: Hello. |
2. html (VAL): sets the HTML content of each matching element. This function cannot be used in XML documents. But it can be used in XHTML documents. Returns a jquery object.
HTML page code: <div> </div> Jquery code: $ ("Div" ).html ("<p> Nice To Meet You </P> "); Result: [<div> <p> Nice To Meet You </P> </div>] |
Second, there are two methods in the text attribute: one with a parameter and the other without a parameter.
1. No parameter text (): obtains the content of all matching elements. The result is a combination of text content contained by all matching elements. Returns a string
Example: HTML page code: <p> <B> Hello </B> fine </P> <P> thank you! </P> Jquery code: $ ("p"). Text (); Result: hellofinethankyou! |
2. text (VAL): Set the text content of all matching elements, similar to HTML, however, encode HTML (replace "<" and ">" with the corresponding HTML Entity ). returns a jquery object.
HTML page code: <p> test paragraph. </P> Jquery code: $ ("p"). Text ("<B> some </B> New text ."); Result: [<p> <B> some </B> New text. </P>] |
Finally, there are two methods in the Val () attribute, one with a parameter and the other without a parameter.
1. No parameter Val (): obtains the current value of the First Matching Element. In jquery 1.2, the value of any element can be returned. Including select. If multiple values are selected, an array containing the selected values is returned.
Returns a string, array
Result:[<P> <B> Single: </B> single <B> multiple: </B> multiple, multiple3 </P>]
Example: // select multiple drop-down boxes, $ ('# multiple'). Val () returns an array // $ ("# Multiple"). Val (). Join (",") to connect each value in the array HTML page code: <P> </P> <br/> <Select id = "single"> <Option> single </option> <Option> single2 </option> </SELECT> <Select id = "multiple" multiple = "multiple"> <Option selected = "selected"> multiple </option> <Option> multiple2 </option> <Option selected = "selected"> multiple3 </option> </SELECT>Jquery code: ("P "). append ("<B> Single: </B>" + $ ("# single "). val () + "<B> multiple: </B>" + $ ("# multiple "). val (). join (",")); |
2. Val (VAL): sets the value of each matching element. In jquery 1.2, this can also assign values to check, select, and radio elements, and return a jquery object.
HTML page code: <Input type = "text"/> Jquery code: $ ("input"). Val ("Hello world! "); Result: Hello world! |