简体   繁体   English

innerHTML在FF中起作用,但在IE7中不起作用

[英]innerHTML works in FF but not in IE7

var row = document.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. 上面的代码适用于FF,但不适用于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. 如果要附加到父项的末尾,则应 insertRow(-1)

Also be careful: the parentNode of a row will be a <tbody> , even if you didn't include a <tbody> tag. 另外要注意:在parentNode一的row会是一个<tbody>即使你不包括<tbody>标签。 This is because in HTML, <tbody> is considered an optional start tag, and implied if omitted before a <tr> . 这是因为在HTML中, <tbody>被认为是可选的开始标记,如果在<tr>之前省略,则暗指。 If you were expecting the parentNode to be the <table> you may be confused. 如果您期望parentNode<table> ,则可能会感到困惑。

(XHTML parses tables more sensibly.) (XHTML更明智地解析表。)

Having said that, your code works for me in IE (given suitable markup and a valid myCounter ). 话虽如此,您的代码在IE(适用的标记和有效的myCounter )中对我有效。 Post a failing test case? 发布失败的测试用例?

Never mind. 没关系。 IE expects document.CreateElement("td") (and also for tr). IE期望document.CreateElement(“ td”)(也适用于tr)。 I added that and it worked. 我添加了它,并且有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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