开发者

getElementsByTagName('TBODY') failes in Chrome

开发者 https://www.devze.com 2023-01-29 17:48 出处:网络
Given the element of a table found with getElementById(), I need to get the body element and add a row to it. This fails in Chrome:

Given the element of a table found with getElementById(), I need to get the body element and add a row to it. This fails in Chrome:

var tabBody = expressionTable.getElem开发者_如何学JAVAentsByTagName('TBODY')[0];

but works in IE.

How can I get the body in all browsers (ie 8, Chrome, FF, and Safari)?

The code looks like this:

var expressionTable = document.getElementById(tableID);
var tabBody = expressionTable.getElementsByTagName('tbody')[0];
var expressionRow = createExpressionRow(FieldTagsValue, row);
tabBody.appendChild(expressionRow);

tabody is 'undefined'

I think it might be because the table starts empty, and Chrome does not have a tbody element for an empty table. Could that be it?

Thanks, Brian


don't rely on anything working in IE - it might behave slightly non-standard. i would also advise not expecting tags You have not declared in Your HTML to be present in DOM.

i would try going with an explicit tbody tag if You do rely on it being present in DOM :

<table>
  <tbody></tbody>
</table>

I know jQuery has some workarounds for handling tables (in IE) - so, unless You're patient to find out all the hacks Yourself, I would go with a library such as jQuery and add table elements like this :

$('#' + tableID).append(expressionRow);
0

精彩评论

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

关注公众号