Parse the HTML string sample code in JS

  • 2020-03-30 02:37:37
  • OfStack

In js directly add HTML statement, js will parse the HTML string into the corresponding HTML statement, and display in the front end.
<span style="font-size:14px;">var el = document.createElement( 'div' ); 
el.innerHTML = "<html><head><title>titleTest</title></head><body><a href='test0'>test01</a><a href='test1'>test02</a><a href='test2'>test03</a></body></html>"; 
el.getElementsByTagName( 'a' ); // Live NodeList of your anchor elements</span> 

Application in jquery:
var el = $( '<div></div>' ); 
el.html("<html><head><title>titleTest</title></head><body><a href='test0'>test01</a><a href='test1'>test02</a><a href='test2'>test03</a></body></html>"); 
$('a', el) // All the anchor elements 

Related articles: