- . HTML () HTML tags for reading and modifying elements
- . Text () to read or modify the plain text content of an element
- . Val () is used to read or modify the value of the form element.
The comparison of the functions of the three methods
- .html (),. Text (),. Val () Three methods are used to read the contents of the selected element; HTML () is used to read the HTML content of the element (including its HTML tag),. Text () is used to read the plain text content of an element, including its descendant elements,. Val () is a value that is used to read the form element's "value". The. and. Text () methods cannot be used on form elements, whereas. val () can only be used on form elements; the HTML () method only reads the first element when it is used on more than one element; the. val () method is the same as. html (), and if it is applied on more than one element, it can only read the The "value" value of a form element, but. Text () is different from theirs, and if. Text () is applied on more than one element, the text content of all the selected elements is read.
- .html (Htmlstring),. Text (TextString) and. Val (value) Three methods are used to replace the contents of the selected element. If three methods are applied to multiple elements at the same time, the contents of all selected elements will be replaced.
- . html (),. Val () can use the return value of the callback function to dynamically change the contents 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 ());
Show as Empty
}
function showhtml ()
{
var div = $ ("#div");
Alert ("showHTML ():" +div.html ());
Shown as:
<p>here is a paragram</p>
<div>here is a big div</div>
}
function Showtext ()
{var div = $ ("#div");
Alert ("Showtext ():" +div.text ());
shown as:
Here is a Paragram
Here is a big DIV
}
Summary: Val ()---is typically used on input instead of other elements to get the value of input or select
HTML () and text () can be used on a variety of elements, but HTML () is equivalent to the return of the source code, that is, the returned content will contain a variety of tags, and text () is the equivalent of the page display is returned, the content returned is out of the various tags between the content
There are two methods in the HTML attribute, one with parameters, one without parameters
1. No parameter HTML (): gets The HTML content of the first matching element. This function cannot be used with XML documents. But it can be used in an XHTML document, which returns a string
Example: HTML page Code:<div><p>hello</p></div> jquery Code: $ ("div"). html (); Result: Hello |
2. Parameter HTML (val): sets the HTML content for each matching element. This function cannot be used with 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>"); Results: [<div><p> nice to meet you</p></div>] |
Second, there are two methods in the Text property, one with a parameter, one without a parameter
1. No parameter text (): Gets the contents of all matching elements. The result is text that is combined with the text content contained by all matching elements. Returned is a string
Example: HTML page Code:<p><b>hello</b> fine</p> <p>thank you!</p> jquery Code: $ ("P"). text (); Results: hellofinethankyou! |
2. Parameter text (val): Sets the text content of all matching elements, similar to HTML (), but encodes the HTML (replaces "<" 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."); Results: [<p><b>Some</b> new text.</p>] |
Finally, there are two methods in the Val () attribute, one with a parameter and one without a parameter.
1. No parameter Val (): Gets The current value of the first matching element. In JQuery 1.2, you can return the value of any element. Includes select. If multiple selections, an array is returned that contains the selected value.
Returns a string, array
Results: [<p><b>single:</b>single<b>multiple:</b>multiple, Multiple3</p>]
Example://Multi-select drop-down box, $ (' #multiple '). Val () returns an array
$ ("#multiple"). Val (). Join (",")) to concatenate each value in the array HTML page code: <P></P><Br/> <SelectId= "single"> <Option>Single</Option> <Option>Single2</Option> </Select> <SelectId= "multiple"Multiple= "multiple"> <OptionSelected= "Selected">Multiple</Option> < Option>multiple2 </option> <option selected= "Selected" >multiple3</ Option> </< Span style= "color: #800000;" >select> jquery Code: ("P"). Append ("<b>Single:</b>" + $ ("#single"). Val () + "<b>Multiple:</b>" + $ ("#multiple"). val (). Join (",")); |
2. Parameter Val (val): sets the value of each matching element. in jquery 1.2, this can also be assigned a value for the Check,select,radio element, returning a JQuery object
HTML page code:
<input type= "Text"/>
jquery Code: $ ("input"). val ("Hello world!");
Result: Hello world!