Js gets and sets the display value of p tag through HTML of and text of methods

  • 2020-03-30 02:57:39
  • OfStack

The 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 get the contents of an element, you can:
 
var p_html = $("p").html(); //Gets the HTML code for the p element

You can also use this method if you want to set the HTML code for an element, but you need to pass it an argument. For example, to set the HTML code for the p element, use the following code:
 
//Sets 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.

The text () method

This method is similar to the innerText attribute in JavaScript and can be used to read or leave the text content in an element. Continue to use the above HTML code, using the text() method to operate on the p element:
 
var p_text = $("p").text(); //Gets the text content of the p element

As with the HTML () method, if you need to set the text content for an element, you also need to pass an argument. For example, to set the text content of p element, the code is as follows:
 
//Sets the text content of the p element
$("p").text(" Welcome to the concise modern magic library "); 

Here are two things to note:

The innerText property in JavaScript does not run in the Firefox browser, and jQuery's text() method supports all browsers.
The text() method works for both HTML and XML documents.

When the list of friends is displayed, the name of the friend is displayed in the < P> < / p> Element setting: < P p id = "${friend. FriendId}" > < / p> , so that js files can be dynamically obtained. P> < / p> $("#p"+userId).text(name);

Related articles: