简体   繁体   English

在克隆表中找到可见的行?

[英]finding visible rows in cloned table?

How can I find the visible rows inside a cloned table? 如何找到克隆表中的可见行?

var divContents = $("#mytable").clone();
divContents.find('tr:visible'); // <-- not working

Please dont tell me to do this: 不要告诉我这样做:

var divContents = $("#mytable tr:visible").clone();

I need to work with the cloned element because I get a copy of it, alter it and then send it to print. 我需要使用克隆的元素,因为我得到了它的副本,对其进行了更改,然后将其发送以进行打印。

jQuery's :visible selector selects elements that consume space in the document. jQuery的:visible选择器选择消耗文档中空间的元素。
Visible elements have a width or height that is greater than zero. 可见元素的宽度或高度大于零。

Your elements aren't even in the document, as they are a cloned set that only exists in memory, so they are by definition not :visible , none of them. 您的元素甚至不在文档中,因为它们是仅存在于内存中的克隆集,因此根据定义它们不是:visible ,都不是。

If what you really want is to select elements that aren't display:none or similar, you can filter based on that 如果您真正想要的是选择不display:none元素display:none或相似,则可以基于该元素进行过滤

var divContents = $("#mytable").clone();

var visible = divContents.find('tr').filter(function(index, elem) {
    return elem.style.display !== 'none' ||
           elem.style.visibility !== 'hidden';
});

visible.css('color', 'red'); // target only visible rows

Visible elements can only be detected when the element is part of the DOM. 仅当可见元素是DOM的一部分时,才能检测到可见元素。 As such you could append the table (well off screen if necessary), work with the visible rows and then remove it (again, if necessary). 这样,您可以追加表格(如果需要,可以离开屏幕),使用可见行,然后将其删除(再次,如果需要)。 Something like this: 像这样:

var $divContents = $("#mytable").clone().css({
    position: 'absolute',
    left: '-10000px'
}).appendTo('body');

var $visibleRows = $divContents.find('tr:visible');
// do something with $visibleRows...

$divContents.remove();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM