JQuery two ways to get and set HTML or TEXT content

  • 2020-03-30 03:03:25
  • OfStack

JQuery provides two apis that can be used directly to add content to elements.

The HTML ()
The text ()

Where HTML () is the addition of HTML content to a specified element
Text () is the addition of text content to a specified element
The difference is that the content in text is plain text and will not be parsed into HTML

If you want to manipulate the following HTML code
 
<body> 
<p></p> 
</body> 

Use the HTML ()
 
$('p').html('<strong>Hello World</strong>, I am a <em><p></em> element.'); 

The page effect is

Hello World, I am a < P> Element.

If I use text()
 
$('p').text('<strong>Hello World</strong>, I am a <em><p></em> element.'); 

The page effect is

< Strong> Hello World< / strong> , I am a < Em> < P> < / em> Element.

Related articles: