IE v。FF:table.tBody [0]有效吗? [英] IE v. FF: Is table.tBodies[0] valid?

查看:207
本文介绍了IE v。FF:table.tBody [0]有效吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从我收集的内容中,tBodies [0]似乎在IE中工作,但不是FF。这是IE的错误吗?我创建了一个小文件来演示不一致,我想知道最好的方法。



HTML:

 < html> 
< body>
< table id =dataGrid>
< / table>
< input type =buttononclick =insertRow(); value =新行>
< / body>
< / html>

此脚本应该在每次点击按钮时添加一行。它在IE中工作,但在Firefox中断开:

 < script type =text / javascriptsrc =/ costplan / script /prototype.js\"></script> 
< script>
function insertRow(){
var objTbl = $('dataGrid')。tBodies [0];
lastRow = objTbl.rows.length;
alert(lastRow);
var newRow = objTbl.insertRow(lastRow);
}
< / script>

tBody [0]无效?我不知道为什么,但是这个代码在FF和IE中都有效:

 < script type =text / javascript src =/ costplan / script / prototype.js>< / script> 
< script>
function insertRow(){
var objTbl = $('dataGrid');
lastRow = objTbl.rows.length;
alert(lastRow);
var newRow = objTbl.insertRow(lastRow);
}
< / script>

这些功能是否正确?基本上,我真的不知道发生了什么(我收集到这些脚本中至少有一个是无效的,但我不知道是哪个或为什么)。

解决方案

tBodies 属性是DOM Level 2标准的一部分,它

您所拥有的不同之处在于,IE 始终注入 TBODY 元素作为你的空表的一个孩子。



Firefox,Chrome和其他浏览器不会这样做,如果元素为空,如果 TABLE 元素至少有一个 TR TH ,它们将隐式地创建 TBODY



您可以添加 TBODY 元素,您的示例将无需工作瑕疵:

 < table id =dataGrid> 
< tbody>
< / tbody>
< / table>


From what I gather, tBodies[0] seems to work in IE, but not FF. Is this an IE mistake? I've created a small file to demonstrate the incosistency, and I would like to know the best way to go about this.

The HTML:

<html>
<body>
 <table id="dataGrid">
 </table>
 <input type="button" onclick="insertRow();" value="New row">
</body>
</html>

This script should add a row every time the button is clicked. It works in IE but breaks in Firefox:

<script type="text/javascript" src="/costplan/script/prototype.js"></script>
<script>    
function insertRow(){
 var objTbl = $('dataGrid').tBodies[0]; 
 lastRow = objTbl.rows.length;
 alert(lastRow);
 var newRow = objTbl.insertRow(lastRow);
}
</script>

Is tBodies[0] invalid? I'm not sure why, but this code works in both FF and IE:

<script type="text/javascript" src="/costplan/script/prototype.js"></script>
<script>
function insertRow(){
 var objTbl = $('dataGrid');
 lastRow = objTbl.rows.length;
 alert(lastRow);
 var newRow = objTbl.insertRow(lastRow);
}    
</script>

Are either of these functions correct? Basically, I don't really know what's going on (I gather that at least one of these scripts is invalid, but I don't know which or why).

解决方案

The tBodies property is part of the DOM Level 2 standard, and it works on both browsers.

The difference you are having is that IE always injects a TBODY element as a child of your empty table.

Firefox, Chrome and other browsers don't do it if the element is empty, if the TABLE element has at least one TR or TH, they will create the TBODY implicitly.

You could add the TBODY element by yourself, and your example will work without problems:

<table id="dataGrid">
  <tbody>
  </tbody>
</table>

这篇关于IE v。FF:table.tBody [0]有效吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆