JQuery gets the implementation code that sets the content or text of an HTML element

  • 2020-03-30 03:24:54
  • OfStack

Here's how it works:

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 script house ~ ~ "); 

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 script house tutorial ~ ~ ");  

Here are two things to note:


Related articles: