• 将HTML格式的String转化为HTMLElement 分类: C1_HTML/JS/JQUERY 2014-08-05 12:01 1217人阅读 评论(0) 收藏


    代码如下:

    <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中定义数据不需要使用[],否则会出错。



    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    tp 30秒超时
    zend studio git 提示错误
    php连接数据库
    php点击排序
    表情符号解析
    js Object.assign 方法
    Web界面简繁体转换
    H5骨架屏实战
    JavaScript heap out of memory问题
    Vue bus插件封装
  • 原文地址:https://www.cnblogs.com/lujinhong2/p/4637268.html
Copyright © 2020-2023  润新知