将HTML格式的String转化为HTMLElement
2015-06-16 15:59
435 查看
代码如下:
1、若将script放到head元素中,则加载脚本时,尚未加载div元素,此时会出现“document.getElementById(...) 为空或者不是对象”。
2、以上代码在chrome中正常支行,输出结果为5,但在IE中输出结果为0.
3、注意在js中定义数据不需要使用[],否则会出错。
<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 水平 垂直 居中简易实现
- HTML常用标签
- html解析器:Html Agility Pack
- word转html
- HTML 5 服务器发送事件
- 用meteor开发网页时html中的seletc 默认选项的设置
- html的meta用法详解
- 在开发Ext 项目中如果遇到 Uncaught TypeError: Cannot read property 'insertAdjacentHTML' of null
- html的meta标签解释
- angular中的ng-bind-html指令和$sce服务
- (五)html部分标签元素补充
- HTML attribute
- Flask在根目录'/'下返回静态HTML模版
- HTML格式自定义OpenCart邮件模板功能插件
- HTML格式自定义OpenCart邮件模板功能插件
- HTML meta头部小结
- Html.TextBoxFor三元判断
- table内增加tr
- HTML字符实体,转义字符串
- html笔记