zoukankan      html  css  js  c++  java
  • IE6-IE9使用JSON、table.innerHTML 问题

    一、在IE兼容模式中以及各具有IE内核的浏览器兼容模式中使用  jQuery 中的JSON函数时出现“JSON未定义”  而切换到浏览器高速模式则正常显示

    解决方案 :

    1.直接在使用的页面中引入json2.js (https://github.com/douglascrockford/JSON-js/blob/master/json2.js)引入方法不再赘述。

    2.在页面的公共js方法里判断

      if(typeof JSON == 'undefined'){ 

      $('head').append($("<script type='text/javascript' src='.../json2.js'>"));  //此处的位置为该js文件所在路径

      }

    二、使用ajax时,经常需要动态生成页面元素,而使用element上属性innerHTML填充页面HTML。但是在使用中发现 table元素的innerHTML时赋值时,在firefox下是好的,但在ie兼容模式中出现未知的运行时错误。错误示例如下:

    发现是在IE6-IE9下,下列元素table,thead,tfoot,tbody,tr,col,colgroup,html,title,style,frameset的innerHTML属性是只读的

    解决方法:调用下面js方法 

    一、表格内没有thead 表头的情况下使用

    function setTableInnerHTML(table, html) {//table 为table对象,html为生成的html字符串
        if (navigator && navigator.userAgent.match(/msie/i)) {
            var temp = table.ownerDocument.createElement('div');
            temp.innerHTML = '<table>' + html + '</table>';//注意此处传进来的html变量包含“<tbody></tbody>”标签    如果HTML变量中没有 则为 '<table><tbody>' + html + '</tbody></table>'
            table.replaceChild(temp.firstChild.firstChild, table.tBodies[0]);//用生成的div中table的tbody替换原table中的tbody
        } else {
            table.innerHTML = html;
        }
    }

     二、表格内带有表头 即table中同时包含 thead以及tbody的情况下

    function setTableInnerHTML(table, html) {
        if (navigator && navigator.userAgent.match(/msie/i)) {
            var temp = table.ownerDocument.createElement('div');
            temp.innerHTML = '<table>' + html + '</table>';
            table.removeChild(table.tBodies[0]);
            var tablenodes = temp.firstChild.childNodes;
            var nodeslength = temp.firstChild.childNodes.length;
            for (var i = 0; i < nodeslength; i++) {
                table.appendChild(tablenodes[0]);
            }
        } else {
            table.innerHTML = html;
        }
    }

  • 相关阅读:
    Can't remove netstandard folder from output path (.net standard)
    website项目的reference问题
    The type exists in both DLLs
    git常用配置
    Map dependencies with code maps
    How to check HTML version of any website
    Bootstrap UI 编辑器
    网上职位要求对照
    Use of implicitly declared global variable
    ResolveUrl in external JavaScript file in asp.net project
  • 原文地址:https://www.cnblogs.com/shijunfeng/p/5066252.html
Copyright © 2011-2022 走看看