码迷,mamicode.com
首页 > Web开发 > 详细

将HTML格式的String转化为HTMLElement

时间:2014-08-05 14:12:10      阅读:221      评论:0      收藏:0      [点我收藏+]

标签:des   http   使用   io   数据   ar   代码   div   

代码如下:

<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>


1、若将script放到head元素中,则加载脚本时,尚未加载div元素,此时会出现“document.getElementById(...) 为空或者不是对象”。
2、以上代码在chrome中正常支行,输出结果为5,但在IE中输出结果为0.
3、注意在js中定义数据不需要使用[],否则会出错。



将HTML格式的String转化为HTMLElement,布布扣,bubuko.com

将HTML格式的String转化为HTMLElement

标签:des   http   使用   io   数据   ar   代码   div   

原文地址:http://blog.csdn.net/jediael_lu/article/details/38383513

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!