将HTML格式的String转化为HTMLElement的实现方法
代码如下:
<metacharset="UTF-8">
<title>Inserttitlehere</title>
</head>
<body>
<divid="content"></div>
<script>
varel=document.createElement("div");
el.innerHTML='<htmlxmlns="http://www.w3.org/1999/xhtml"><head><metahttp-equiv="Content-Type"content="text/html;charset=utf-8"/><title>titleValue</title><metaname="description"content="Itisagoodwaytolearnscience."/><metaname="keywords"content="Symfony2,Redis,PHP"/><metaname="author"content="CSDN.NET"/><metaname="Copyright"content="CSDN.NET"/></head><body>hello</body></html>';
vardescElements=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中定义数据不需要使用[],否则会出错。
热门推荐
1 训练狗狗憋尿
2 狗狗追踪训练
3 狗狗训练要多少钱
5 深圳狗狗训练
6 训练狗狗唤回
7 狗狗叫怎么训练
8 狗狗训练班
10 狗狗社会化训练
11 狗狗叫回训练
12 广州狗狗训练学校
13 狗狗训练躺下
14 狗狗随行训练
15 训练狗狗巡回
16 狗狗要怎么训练
17 狗狗喂食训练
18 训练狗狗躺下