开发者

innerHTML works in FF but not in IE7

var row = d开发者_Python百科ocument.getElementById('my_id');
var newRow = row.parentNode.insertRow();
newRow.Id = myCounter;

var newCell2 = newRow.insertCell(0);
newCell2.innerHTML = 'some text';  

I'm trying to create a new row and a new cell in that row. The above code works in FF but not in IE. What am I doing wrong?


var newRow = row.parentNode.insertRow();

Should be insertRow(-1) if you mean to append to the end of the parent.

Also be careful: the parentNode of a row will be a <tbody>, even if you didn't include a <tbody> tag. This is because in HTML, <tbody> is considered an optional start tag, and implied if omitted before a <tr>. If you were expecting the parentNode to be the <table> you may be confused.

(XHTML parses tables more sensibly.)

Having said that, your code works for me in IE (given suitable markup and a valid myCounter). Post a failing test case?


Never mind. IE expects document.CreateElement("td") (and also for tr). I added that and it worked.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜