Home > Web Front-end > JS Tutorial > How to convert HTML formatted String into HTMLElement_javascript skills

How to convert HTML formatted String into HTMLElement_javascript skills

WBOY
Release: 2016-05-16 16:40:02
Original
2220 people have browsed it

The code is as follows:

<meta charset="UTF-8"> 
<title>Insert title here</title> 
</head> 
<body> 
<div id="content"></div> 
<script> 
var el = document.createElement("div"); 
el.innerHTML = ' <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>titleValue</title> <meta name="description" content="It is a good way to learn science." /> <meta name="keywords" content="Symfony2,Redis,PHP" /> <meta name="author" content="CSDN.NET" /> <meta name="Copyright" content="CSDN.NET" /> </head> <body> hello</body> </html>'; 
var descElements = el.getElementsByTagName("head"); 
document.getElementById("content").innerHTML = descElements.length; 
</script> 
</body> 
</html>
Copy after login

1. If the script is placed in the head element, when the script is loaded, the div element has not been loaded, and "document.getElementById(...) is empty or not an object" will appear.
2. The above code works normally in chrome, and the output result is 5, but in IE, the output result is 0.
3. Note that you do not need to use [] when defining data in js, otherwise an error will occur.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template