开发者

Mysterious runtime error in IE

IE version : 8.0.6001.18702

<tr id="week-days"></tr>

function displayWeekDay( lang )
{
    var txt = '<td>num</td>';
    for ( i = 0; i < langs[lang].day.length; i++ )
        if ( i == 5 )       txt += '<td class="red"><b>'   + langs[lang].day[i] + '</b></td>';
        else if ( i == 6 )  txt += '<td class="red"><b>'   + langs[lang].day[i] + '</b></td>';
        else                txt += '<td><b>'               + langs[lang].day[i] + '</b></td>';
    document.getElementById("week-days").innerHTML = txt;
}
开发者_开发技巧

The error is in the last line - document.getElementById("week-days").innerHTML = txt;

I`m a little bit confused - there is an error, but everything is displaying properly. There is no error on Mozilla, Chrome, Safari. Any idea will apreciated.

edit

Mysterious runtime error in IE


From MSDN: "the innerText and innerHTML properties of the table and tr objects are read-only" http://msdn.microsoft.com/en-us/library/ms532998(v=vs.85).aspx

Solution is to create a tr element with the dom, or to build a full temporary table using html, then move the row to the destination table. Like the following:

/** Appends a row to the given tbody */
function appendRow(tbody, trHTML) {
  var tempNode = document.createElement('div');
  tempNode.innerHTML = "<table><tr>" + trHTML + "</tr></table>";
  var tempTable = tempNode.firstChild;
  tbody.appendChild(tempTable.rows[0] );
}

You could modify the function to specify the location to insert to, or another tr to insert before or after.

See Can't set innerHTML on tbody in IE


This sounds like an IE issue that has existed for quite some time. I've ran into it myself in the past. You might want to try creating the nodes via DOM methods.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜