In jquery. HTML () is used for reading and modifying elements of the HTML tag,. Text () to read or modify the plain text content of the element,. Val () is used to read or modify the value of the form element.
1.HTML
HTML (): Gets the HTML content of the first matching element. This function cannot be used with XML documents. But can be used in XHTML documents
HTML (val): Sets the HTML content of each matching element. This function cannot be used with XML documents. But it can be used in XHTML documents.
2.TEXT
Text (): Gets the contents of all matching elements.
The result is text that is combined with the text content contained by all matching elements. This method is valid for both HTML and XML documents.
Text (val): Sets the textual content of all matching elements
Similar to HTML (), but encodes HTML (replaces "<" and ">" with the corresponding HTML entity).
3.VAL
Val (): Gets the current value of the first matching element.
Val (val): Sets the value of each matching element.
The above content is copied in the jquery help document.
Finally, summarize these three methods:
- . 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 these three methods in function:
- . HTML (),. Text (),. Val () Three methods are used to read the contents of the selected element;
- Just. HTML () is used to read the HTML content of an element (including its HTML tag), and. 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, while. val () can only be used on form elements;
- In addition, when the HTML () method is used on more than one element, only the first element is read;
- The. Val () method is the same as. html (), and can only read the value of the first FORM element if it is applied on more than one element, but. Text () is not the same as they are, 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, and if three methods are applied on more than one element at the same time, the contents of all the selected elements will be replaced.
- . HTML (),. Text (),. Val () can use the return value of the callback function to dynamically change the contents of multiple elements.
< reference: "JQuery". An overview of the HTML (),. Text (), and. Val () and Usage & analysis of the HTML (), text (), Val () difference in jQuery () >
HTML () in jquery, text (), Val () difference