IE shows run time error for innerHTML

后端 未结 9 1992
谎友^
谎友^ 2020-12-11 09:54

I have a jsp page in which rows are created dynamically to a table using java script. It is working fine in all the browsers except IE. In IE it is showing the error Unknown

9条回答
  •  时光说笑
    2020-12-11 10:22

    This error shows up, because several elements' innerHTML property, including are read-only [source] (Tested in IE 6-8). To solve this issue, the best way is to use the insertCell method:

    An universal "lazy" method: (demo: http://jsfiddle.net/VLjhW/2/)

    // Variables mystring2, rowCount and table as defined in the question.
    var tmp = document.createElement('div');         // <-- Placeholder
    tmp.innerHTML = '' + mystring2 + '
    '; var row = tmp.firstChild.rows[0]; // <-- Created "real" row var newrow = table.insertRow(table.rows.length); // <-- New dummy row newrow.parentNode.replaceChild(row, newrow); // <-- Replace dummy with real row


    Another method:

    Demo: http://jsfiddle.net/VLjhW/

    // Array of innerHTML properties for each cell
    var cells = ['

    Type    

    ', '', '', '

    Description    

    ', '']; // Array of `class=` attributes for each cell var cellClasses = ['formlabel', 'formfield', 'formgap', 'formlabel', 'formfield']; var table = document.getElementById('t1'); // <-- table var tr = table.insertRow(table.rows.length-1); // <-- Last row for (var i=0; i

提交回复
热议问题