Home >Web Front-end >JS Tutorial >Problems caused by using JSON and table.innerHTML in IE6-IE9_javascript skills

Problems caused by using JSON and table.innerHTML in IE6-IE9_javascript skills

WBOY
WBOYOriginal
2016-05-16 15:24:121099browse

1. When using the JSON function in jQuery in IE compatibility mode and browser compatibility mode with IE kernel, "JSON undefined" appears, but switching to the browser high-speed mode displays normally

Solution:

1. Directly introduce json2.js into the page you are using (https://github.com/douglascrockford/JSON-js/blob/master/json2.js). The introduction method will not be described in detail.

2. Determine in the public js method of the page

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

2. When using ajax, it is often necessary to dynamically generate page elements, and use the innerHTML attribute on the element to fill the page HTML. However, when it is found that the innerHTML of the table element is assigned a value during use, it is fine under Firefox, but an unknown runtime error occurs in IE compatibility mode. Examples of errors are as follows:


It was found that under IE6-IE9, the innerHTML attributes of the following elements table, thead, tfoot, tbody, tr, col, colgroup, html, title, style, frameset are read-only

Solution: Call the following js method

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

The above content is the problems caused by the use of JSON and table.innerHTML in IE6-IE9 shared by the editor. I hope you like it.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn