IE v。FF:table.tBody [0]有效吗? [英] IE v. FF: Is table.tBodies[0] valid?
问题描述
从我收集的内容中,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>
这些功能是否正确?基本上,我真的不知道发生了什么(我收集到这些脚本中至少有一个是无效的,但我不知道是哪个或为什么)。
您所拥有的不同之处在于,IE 始终注入 Firefox,Chrome和其他浏览器不会这样做,如果元素为空,如果 您可以添加 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: This script should add a row every time the button is clicked. It works in IE but breaks in Firefox: Is tBodies[0] invalid? I'm not sure why, but this code works in both FF and IE: 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 The difference you are having is that IE always injects a Firefox, Chrome and other browsers don't do it if the element is empty, if the You could add the
这篇关于IE v。FF:table.tBody [0]有效吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! tBodies
属性是DOM Level 2标准的一部分,它
TBODY
元素作为你的空表的一个孩子。
TABLE
元素至少有一个 TR
或 TH
,它们将隐式地创建 TBODY
。
TBODY
元素,您的示例将无需工作瑕疵:
< table id =dataGrid>
< tbody>
< / tbody>
< / table>
<html>
<body>
<table id="dataGrid">
</table>
<input type="button" onclick="insertRow();" value="New row">
</body>
</html>
<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>
<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
property is part of the DOM Level 2 standard, and it works on both browsers.TBODY
element as a child of your empty table.TABLE
element has at least one TR
or TH
, they will create the TBODY
implicitly.TBODY
element by yourself, and your example will work without problems:<table id="dataGrid">
<tbody>
</tbody>
</table>