Why is my hidden <tr> not really hidden?

泪湿孤枕 提交于 2019-12-05 09:28:07

The :visible selector does not test the display style property, you want to use :hidden instead, the 1.3.2 release notes say:

...if your element's CSS display is "none", or any of its parent/ancestor element's display is "none", or if the element's width is 0 and the element's height is 0 then an element will be reported as hidden.

These will correctly select your visible rows:

$('.trClass:not(:hidden)').each(function() { 
    alert('visible'); 
});

or:

$('.trClass').each(function() { 
    if(!$(this).is(':hidden')) {
        alert('visible'); 
    }
});

or:

$('.trClass').filter('not:(:hidden)').each(function() { 
    alert('visible');
});

hide sets the style to display="none". The release notes for jQuery 1.3.2 also say:

In jQuery 1.3.2 an element is visible if its browser-reported offsetWidth or offsetHeight is greater than 0.

so I guess in this case the :visible selector is erroneously not matching anything because the rows are not occupying any space according to the calculations performed, despite the fact that their CSS display property is not set to none. Conversely, :hidden correctly matches elements with style="display:none" so testing for non :hidden elements works just fine.

You've found a legitimate bug. It's broken in 1.3.2 but now fixed in trunk.

According to Resig:

we already look for the case of 'tr' which has the same problem, in IE

Thought you'd like to know...

Not sure if this matters, but doesn't hide() set display: none; and not visible: hidden? Meaning that a hidden row is still visible, it just isn't displayed...

most likely your trClass collides with the display: none that .hide() sets. when a tag has both class attribute and style attribute only on will be applied. you should inpsect closely your trClass and take out display: stuff from it.

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