jQuery each tr.children is undefined in Firefox 3.0

时光怂恿深爱的人放手 提交于 2019-12-06 06:29:27

Why not use jQuery to traverse the DOM elements instead.

var bodyRows = $("#resultsTable tbody tr");
bodyRows.each(function(){
    var thirdCell = $(this).find('td').eq(2);
    if(!filterPattern.test($.trim(thirdCell.html()))){
        this.style.display = 'none';
    } else {
        this.style.display = '';
    }
});

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 property childNodes for accessing children. The problem with childNodes 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.

if(!filterPattern.test($.trim(this.children[2].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.

Do you explicitly add a

<tbody>
   ...
</tbody>

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!