jQuery 每个 tr.children 在 Firefox 3.0 中未定义
我正在使用一段代码来过滤表中的行。 它在除 Firefox v3.0.x 之外的所有浏览器中都能完美运行(在 3.1 beta 2 中也能正常运行)。 当我在 Firefox 3.0.x 中运行该代码片段时,它显示 children is undefined
。 我也在使用 jQuery v1.2.6。
代码片段:
var bodyRows = $("#resultsTable tbody tr");
bodyRows.each(function(n){
if(!filterPattern.test($.trim(this.children[2].innerHTML))){ //errors here
this.style.display = 'none';
}
else {
this.style.display = '';
}
});
该代码选择所有表格行,然后循环遍历它们,测试第三列的innerHTML 文本。 如果 RegEx 测试失败,则隐藏该行,否则显示该行。
有没有人见过这个问题和/或知道如何让它工作?
谢谢
编辑: 以下是该表的 HTML 标记。 为简洁起见,尽管填充了更多记录,但我只给出了 2 条记录。
<table id="resultsTable" cellpadding="0" cellspacing="0">
<thead>
<tr>
<th>First</th>
<th>Last</th>
<th>City</th>
<th>State</th>
<th>Zip</th>
<th>Email</th>
<th> </th>
</tr>
</thead>
<tbody id="resultsBody">
<tr>
<th>James</th>
<th>Eggers</th>
<th>SomeCity</th>
<th>IA</th>
<th>55555</th>
<th>[email protected]</th>
<th> </th>
</tr>
<tr>
<th>John</th>
<th>Doe</th>
<th>SomeCity</th>
<th>KY</th>
<th>88888</th>
<th>[email protected]</th>
<th> </th>
</tr>
</tbody>
</table>
I have a snippet of code that I'm working with to filter rows in a table. It works perfectly in every browser other than Firefox v3.0.x (works fine with 3.1 beta 2). When I run the snippet in Firefox 3.0.x, it says that children is undefined
. I am using jQuery v1.2.6 also.
Code Snippet:
var bodyRows = $("#resultsTable tbody tr");
bodyRows.each(function(n){
if(!filterPattern.test($.trim(this.children[2].innerHTML))){ //errors here
this.style.display = 'none';
}
else {
this.style.display = '';
}
});
The code selects all table rows and then loops through them, testing the innerHTML text of the 3rd column. If the RegEx test fails, the row is hidden, else it is displayed.
Has anyone seen this issue and / or know how to get it working?
Thanks
EDIT:
Here is the HTML markup for the table. For brevity, I'm only giving 2 record in it though more are populated.
<table id="resultsTable" cellpadding="0" cellspacing="0">
<thead>
<tr>
<th>First</th>
<th>Last</th>
<th>City</th>
<th>State</th>
<th>Zip</th>
<th>Email</th>
<th> </th>
</tr>
</thead>
<tbody id="resultsBody">
<tr>
<th>James</th>
<th>Eggers</th>
<th>SomeCity</th>
<th>IA</th>
<th>55555</th>
<th>[email protected]</th>
<th> </th>
</tr>
<tr>
<th>John</th>
<th>Doe</th>
<th>SomeCity</th>
<th>KY</th>
<th>88888</th>
<th>[email protected]</th>
<th> </th>
</tr>
</tbody>
</table>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
为什么不使用 jQuery 来遍历 DOM 元素呢?
如果您只想返回没有任何可能标记的文本,也可以使用“
.text()
”。属性
children
是 IE 独有的 DOM 属性,其他浏览器没有(据我所知)。 Firefox 使用标准属性childNodes
来访问子节点。childNodes
的问题在于它认为空格和文本是一个节点(或者至少 Firebug 是这么说的),这使得它(在我看来)非常难以处理。 如果您有 JavaScript API,您应该利用它,这样您就不必处理浏览器 DOM 遍历技术之间的差异。Why not use jQuery to traverse the DOM elements instead.
You could also use '
.text()
' if you just want the text without any possible markup to be returned.The property
children
is an IE only DOM property which no other browser has (to my knowledge). Firefox uses the standard propertychildNodes
for accessing children. The problem withchildNodes
is that it considers whitespace and text to be a node (or at least Firebug says so) which makes it (in my opinion) very difficult to deal with. If you have a JavaScript API, you should take advantage of it so that you don't have to deal with the differences between browsers' DOM traversal techniques.当 jQuery 调用“each”回调时,“this”是直接浏览器 DOM 节点,而不是 jQuery 对象。
之所以会出现混乱,是因为 jQuery 在其 DOM 包装器上提供了“子”方法,而 IE 在其本机 DOM 节点上提供了非标准“子”集合,但这两个接口几乎完全不兼容。
因此,对于 jQuery 版本,请使用 $(this).children()[2] 或类似内容,对于标准 DOM 版本,请使用 this.getElementsByTagName('td')[2] 。
(假设您打算将表数据元素设置为“td”而不是“th”,您可能会这样做。此外,您可能希望获取单元格的原始文本而不是innerHTML 版本,因为innerHTML 版本可能会以意想不到的方式转义字符。 )
When an 'each' callback is invoked by jQuery, 'this' is a direct browser DOM node, not a jQuery object.
Confusion occurs because jQuery offers a 'children' method on its DOM wrappers, and IE offers a non-standard 'children' collection on its native DOM nodes, but the two interfaces are almost totally incompatible.
So use $(this).children()[2] or similar for the jQuery version, or this.getElementsByTagName('td')[2] for the standard DOM version.
(Assuming you meant to make the table data elements 'td' rather than 'th', which you probably did. Also you probably want to grab the raw text of the cell rather than the innerHTML version which may have characters escaped in unexpected ways.)
在尝试访问第三个元素之前,您可能需要检查子元素的数量。
you might want to check the number of elements in children before trying to access the 3rd one.
您是否明确地将
标签添加到表中? 如果没有,我会说从以下位置删除“tbody”部分: $("#resultsTable tbody tr");
只是 $("#resultsTable tr");
我很好奇这个版本的 Firefox 是否没有隐式地为您创建它。
Do you explicitly add a
tag to your table? If not, I would say to drop the 'tbody' portion from: $("#resultsTable tbody tr");
to just $("#resultsTable tr");
I'm curious if this version of Firefox isn't implicitly creating it for you.