Differences between html (), text (), val () in JQuery, and jqueryval
1. HTML
Html (): gets the html content of the First Matching Element. This function cannot be used in XML documents. But it can be used in XHTML documents.
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.
2. TEXT
Text (): Get the content of all matching elements.
The result is a combination of text content contained by all matching elements. This method is effective for both HTML and XML documents.
Text (val): Set the text content of all matching elements.
Similar to html (), but the encoding HTML (replace "<" and ">" with the corresponding HTML Entity ).
3. VAL
Val (): obtains the current value of the First Matching Element.
Val (val): sets the value of each matching element.
The above content is copied in the help document of JQuery, and it is not nonsense. Below are some exercises you have made. The Code is as follows:
During the exercises, I found another difference between html and text.
When html () is used to remove the content of an element, the format below the selected element can also be obtained.
For example, <div id = "divShow"> <B> <I> Write Less Do More </I> </B> </div>
If we use var strHTML = $ ("# divShow" cmd.html,
Result: <B> <I> Write Less Do More </I> </B>
If we use var strHTML2 =$ ("# divShow B I" cmd.html ();
The result is Write Less Do More.
Text does not have the first case,
If var strText = $ ("# divShow"). text ();
The result is Write Less Do More.
<% @ Page language = "java" import = "java. util. * "pageEncoding =" UTF-8 "%> <% String path = request. getContextPath (); String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/"; %> <! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN">
You can also verify it by yourself. The above is my experiment, and the JQuery I use is 1.6
Summary:
. Html () uses HTML tags for reading and modifying elements
. Text () is used to read or modify the plain text content of an element.
. Val () is 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.
In jquery, html (), text (), and val () are different.
Html means you can add tags such as <a> </a> and <p> </p>.
Only text can be written. If the above mark is written, it will be output as text.
Val is an attribute. Only objects with this attribute can be called.
What are the differences between text (), html (), and val () in jQuery?
Text (): obtains or modifies the text of a specified element.
Html (): gets or modifies the html elements and text of a specified element.
Val (): Get or change the value of a specified Element (usually a form element)
The above three are all the syntaxes in the jquery class library.
The second problem is that there is basically no difference. The function is to obtain the value of the current object (usually a form element)
However, this. value is the native Syntax of js, and $ (this). val () is the syntax of jquery.
Using this. value removes the need to introduce any library files, while $ (this). val () requires the introduction of jquery library files.