The html () method can be used to read or set the HTML content of an element. The text () method can be used to read or disable the text content of an element.
Html () method
This method is similar to the innerHTML attribute in JavaScript and can be used to read or set the HTML content in an element. To obtain the content of an element, you can:
Var p_html = $ ("p" ).html (); // obtain the HTML code of the p element
If you need to set the HTML code of an element, you can also use this method, but you need to pass a parameter for it. For example, to set the HTML code of the p element, you can use the following code:
// Set the HTML code for the p element $ ("p" ).html ("welcome to the concise modern magic library ~~ ");
Note: The html () method can be used for XHTML documents, but not for XML documents.
Text () method
This method is similar to the innerText attribute in JavaScript. It can be used to read or leave the text content in an element empty. Continue to use the preceding HTML code and use the text () method to operate the p element:
Var p_text = $ ("p"). text (); // obtain the text content of the p element.
Like the html () method, if you need to set text content for an element, you also need to pass a parameter. For example, to set text content for the p element, the Code is as follows:
// Set the text content of the p element $ ("p"). text ("welcome to the concise modern magic library ~~ ");
Pay attention to the following two points:
The innerText attribute in JavaScript cannot be run in the Firefox browser, while jQuery's text () method supports all browsers.
The text () method is effective for both HTML and XML documents.
When a friend list is displayed
Element settings:
In this way, js files can be dynamically obtained.
Element id, and the value $ ("# p" + userId). text (name) is successfully assigned through the text () method );