简体   繁体   中英

Javascript problems with Internet Explorer

I'm having rows,for which i need to append controls dynamically. code for it is as follows

<script type="text/javascript">
function AddRowContent()
{
    //Here trRow is a id of row for which I need to append the data.
    var d=document.getElementById("trRow");
    d.innerHTML="<td>Aliases</td>";     
}
</script>

Code is working fine with other browsers, but it's not working properly with Internet Explorer. Please Let me know solution or better way to do this.

In IE, innerHTML on tr elements is readOnly.

Solution:

var d=document.getElementById("trRow");
var td = document.createElement('td');
td.innerHTML = "Aliases";
d.appendChild(td);

Yes, innerHTML property is readonly for <tr> element in IE, as MSDN described:

The innerHTML property is read-only on the col, colGroup, frameSet, html, head, style, table, tBody, tFoot, tHead, title, and tr objects.

You can change the value of the title element using the document.title property.

To change the contents of the table, tFoot, tHead, and tr elements, use the table object model described in Building Tables Dynamically . However, to change the content of a particular cell, you can use innerHTML.

So to manipulate your table, try read Building Tables Dynamically and find a solution, for your simple case, use DOM API could be a compatible solution:

var td = document.createElement('td');
td.innerHTML = 'Aliases';
// Remove all existing <td> element
while (d.firstChild) {
    d.removeChild(d.firstChild);
}
d.appendChild(td);
var firstRow=document.getElementById("myTable").rows[0];
var x=firstRow.insertCell(-1);
x.innerHTML="New cell"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM