FireFox对TABLE中各元素的firstChild属性的处理

前几天测试一个小程序,其中需要获取表格中某行第一个单元格自定义的属性值,很自然地就用到:

tr.firstChild.getAttribute('bill_id');

这行代码在IE6上可以正确执行,但是在FireFox3.5上却没有结果。错误提示:不支持该属性或方法。根据经验判断,应该是tr或者tr.firstChild二者中有一个没有获取正确的对象。于是使用tr.nodeName和tr.firstChild.nodeName进行测试,前者是tr对象没有错误,而后者的值却是#text;而此时使用tr.firstChild.sibling获得就是第一个td对象。

当时就想,FireFox是不是对Table中所有元素都采用了这样的处理,即不是按照table>tbody>tr/th>td的顺序,而是table>#text|tbody>#text|tr>#text|td。于是就写了下面的简单测试程序,进行验证。

<html>
<head>
<title>firstChild Test</title>
<script language="javascript"><!--
function testTable(){
var table = document.getElementById('table1');
var firstChildOfTable = table.firstChild.nodeName;
var tbody = table.getElementsByTagName('tbody')[0];
var firstChildOfTbody = tbody.firstChild.nodeName;
var tr = tbody.getElementsByTagName('tr')[0];
var firstChildOfTr = tr.firstChild.nodeName;
alert('table>' + firstChildOfTable + '|tbody>' + firstChildOfTbody + '|tr>' + firstChildOfTr);
}

// --></script>
</head>
<body>
<table id="table1" border="1">
<tr>
<td>cell1</td>
<td>cell2</td>
</tr>
<tr>
<td>cell3</td>
<td>cell4</td>
</tr>
</table>
<input type="button" value="Test" onClick="testTable()">
</body>
</html>

 

 这是得到结果如下图所示:

从图中可以明显看到TBody的firstChild值就是TR,这和原来的设想并不一样。

仔细观察代码可以发现,TBODY并没有实际写在代码中,而是由浏览器自动加上,那么会不会是这样原因导致的?于是把表格代码改为下面的形式:

<table id="table1" border="1">
<tbody>
<tr>
<td>cell1</td>
<td>cell2</td>
</tr>
<tr>
<td>cell3</td>
<td>cell4</td>
</tr>
</tbody>
</table>

 再次运行,结果如图:

 

证实了上面的假设,即tbody是由FireFox自动加上的。

 

于是又想,如果表格完全是由JavaScript动态生成,firstChild是不是就不是#text了呢?通过下面的程序测试一下:

function testTable2(){
var table = document.createElement('table');
var tbody = document.createElement('tbody');
table.appendChild(tbody);
var tr = document.createElement('tr');
tbody.appendChild(tr);
var td = document.createElement('td');
tr.appendChild(td);
document.body.appendChild(table);
var firstChildOfTable = table.firstChild.nodeName;
var firstChildOfTbody = tbody.firstChild.nodeName;
var firstChildOfTr = tr.firstChild.nodeName;
alert('table>' + firstChildOfTable + '|tbody>' + firstChildOfTbody + '|tr>' + firstChildOfTr);
}

这次得到的结果:

 

这就和IE中的结果保持一致。

 

现在知道了FireFox和IE中对于Table中各元素的firstChild属性有不同的处理,那么就要避开使用firstChild以及其他child相关属性,而使用:getElementById(), getElementsByTagName()此类的方法,使程序具有跨浏览器特性。

时间: 2024-08-01 14:19:28

FireFox对TABLE中各元素的firstChild属性的处理的相关文章

FireFox对TABLE中各元素的firstChild属性的处理(续)

听说HTML 5.0已经有一段时间了,但是并不知道它到底做了什么改进,这两天就看了下Specification,发现了下面一段话: The markup snippet at the top of this section would be turned into the following DOM tree: DOCTYPE: html html head #text : ⏎␣␣ title #text : Sample page #text : ⏎␣ #text : ⏎␣ body #tex

Javascript遍历table中的元素示例代码

 这篇文章主要介绍了Javascript如何遍历table中的元素,需要的朋友可以参考下       例如: <table id=tb> <tr><th> </th><th> </th><th> </th><th> </th></tr> <tr><td> </td><td> </td><td> </

Javascript遍历table中的元素示例代码_javascript技巧

例如: <table id=tb> <tr><th> </th><th> </th><th> </th><th> </th></tr> <tr><td> </td><td> </td><td> </td><td> </td></tr> <tr>&

JavaScript中为元素加上name属性的方法_javascript技巧

今天遇到个小问题, 在构建 DOM 时, IE 中不能通过 element.setAttribute('name', _variable); 和 element.name = _variable; 这样的形式来为元素加上 name 属性, 无论是 IE6 还是 IE7. (IE8 是可以的, 但 IE8rc1 不行) 后来我查看了 MSDN, 得到信息如下: 复制代码 代码如下: Internet Explorer 8 and later can set the NAME attribute a

javascript 导出数据到Excel(处理table中的元素)_javascript技巧

做法: 修改Input的outerHTML; 具体例子: 复制代码 代码如下: function resetInput() { var controls = document.getElementsByTagName('input'); for(var i=0; i<controls.length; i++){ if(controls[i].type=='text') { if(controls[i].value =="") { controls[i].outerHTML=&qu

js获取浏览器高度 窗口高度 元素尺寸 偏移属性的方法_javascript技巧

如下所示: screen.width screen.height screen.availHeight //获取去除状态栏后的屏幕高度 screen.availWidth //获取去除状态栏后的屏幕高度 一.通过浏览器获得屏幕的尺寸 二.获取浏览器窗口内容的尺寸 //高度 window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight //宽度 window.innerWidth

JS获取网页中HTML元素的几种方法分析

js|网页 getElementById getElementsByName getElementsByTagName 大概介绍 getElementById ,getElementsByName ,getElementsByTagName 后两个是得到集合,byid只是得到单个对象 getElementById 的用法 举个例子: <a id="link1" name="link1" href=http://www.webjx.com>网页教学网<

jQuery实现table中的tr上下移动并保持序号不变的实例代码_jquery

jQueryMoveTr.html 代码如下: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <TITLE>jQuery-bhang</TITLE> <script type="text/javascript" src="jquery-1.6.2.js"></sc

index-lua的元表怎么遍历不到,而且为什么也不能改变表中的元素的值呢,求详解?

问题描述 lua的元表怎么遍历不到,而且为什么也不能改变表中的元素的值呢,求详解? local function tab(t) local proxy = {} local mt = { __index = t, __newindex = function(t, k, v) error("attempt to update readonly a table") end } setmetatable(proxy, mt) return proxy end local days = tab