使用 jquery 选择 :visible 表格单元格不起作用?
我有一个表格,可以在较小的屏幕上自动隐藏列。在某些列上,标题跨越两行,如下所示:
<table cellspacing="0">
<thead>
<tr>
<th colspan="4" class="essential">Bestellung</th>
</tr>
<tr>
<th>Nummer</th>
<th>Datum</th>
<th class="essential">Menge</th>
<th class="essential">Wert</th>
</tr>
</thead>
...
我有一些逻辑,以便用户可以隐藏/显示他想要的列。我用它来确保如果第二个标题行中的所有列都被隐藏,则第一个标题行中的相应元素也会隐藏,当然反过来=切换第二行元素后立即显示第一行元素。
var doubles = thead.find( "tr:first-child th, TR:first-child TH" ).not('[rowspan=2]');
if ( thead.find("tr, TR").length > 1 && thead.find("tr:last-child th:visible, TR:last-child TH:visible" ).length === 0 ) {
doubles.hide();
} else {
doubles.show();
}
:visible 选择器在较大的屏幕尺寸上工作正常。在较小的尺寸上,我在加载时自动隐藏一些列,然后选择器不再工作。
在上面的示例中,.essential 类是可见的。然而,当我切换任何类时,以下总是返回 0:我不明白为什么控制台显示 0,0,0,尽管 1,2,3 或所有 4 个元素都是可见的。
也许有人可以指出我可能的原因。
console.log( thead.find("tr:last-child th:visible").length );
console.log( thead.find("tr:last-child th").filter(":visible").length );
console.log( thead.find("tr:last-child th[display=table-cell]").length );
还有另一种方法来选择可见元素吗?
I have a table which automatically hides columns on smaller screens. On some columns the header is spanning two rows like so:
<table cellspacing="0">
<thead>
<tr>
<th colspan="4" class="essential">Bestellung</th>
</tr>
<tr>
<th>Nummer</th>
<th>Datum</th>
<th class="essential">Menge</th>
<th class="essential">Wert</th>
</tr>
</thead>
...
I have some logic so the user can hide/show the columns he wants. I use this to make sure that if all columns in the second header row are hidden, the corresponding element in the first header row also hides and of course the other way around = show the first row element as soon as one second row element is toggled.
var doubles = thead.find( "tr:first-child th, TR:first-child TH" ).not('[rowspan=2]');
if ( thead.find("tr, TR").length > 1 && thead.find("tr:last-child th:visible, TR:last-child TH:visible" ).length === 0 ) {
doubles.hide();
} else {
doubles.show();
}
The :visible selector works ok on larger screen sizes. On smaller size, I auto-hide some columns on load and then the selector does not work anymore.
In the above example the .essential classes are visible. Yet when I toggle any of the classes the following always returns 0: I don't understand why the console says 0,0,0 although 1,2,3 or all 4 elements are visible.
Maybe someone can point me to the possible cause.
console.log( thead.find("tr:last-child th:visible").length );
console.log( thead.find("tr:last-child th").filter(":visible").length );
console.log( thead.find("tr:last-child th[display=table-cell]").length );
is there another way to select visible elements?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请尝试使用以下代码:
请注意,最新版本的 Sizzle 没有
:visible
。请参阅此处的 JSFiddle。Try the following code instead:
Note that the latest version of Sizzle doesn't have
:visible
. See a JSFiddle here.