Js USES the HTML of or text of method to get the value that sets the display of the p tag

  • 2020-03-30 03:39:54
  • 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(); // To obtain p Elements of the HTML code 

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(); // To obtain p The textual content of the 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 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: