javascript - jQuery:如何映射隐藏的元素

标签 javascript jquery html

我的表中有一些 TR,其类为 parent_0。我正在使用 jQuery 的 map() 来获取 map 中未隐藏的所有元素。

DOM:

<table id="tableID" class="tableClass">
<tbody>
    <tr>
        <th width="3px">Header1</th>
        <th align="left">Header2</th>
    </tr>
    <tr align="left">
        <td>Data1</td>
        <td><a <i id="1" class="parent_0" ></i>></a>Data12</td>
    </tr>
    <tr align="left" style="display: none;">
        <td>Data2</td>
        <td><a <i id="2" class="parent_0"></i>></a>Data22</td>
    </tr>
</tbody>

jQuery:

$( document ).ready(function(){ 
    allChildern = $(".parent_0").map(function() {
        return this.id
    }).get();
    alert(allChildern)

    var allSubChildern = $(".parent_0").map(function() {
       if($('#'+this.id).closest('tr').is(':visible')){
           return this.id //its working but taking so much time for 1k records.
       }
    }).get();
    alert(allSubChildern)
});

是否有任何函数可以映射仅可见的元素。

这里是Fiddle 。谢谢。

最佳答案

您可以使用.filter()获取所有可见行,然后继续使用.map()

Reduce the set of matched elements to those that match the selector or pass the function's test.

var allSubChildern = $(".parent_0").filter(function(){
    return $(this).closest('tr').is(':visible');
}).map(function() {
    return this.id;
}).get();
alert(allSubChildern)

或者,您也可以使用

var allSubChildern = $("#tableID tr:visible .parent_0").map(function() {
    return this.id;
}).get();

关于javascript - jQuery:如何映射隐藏的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35055432/

相关文章:

javascript - 如何将此 Javascript 分解为单独的 CSS 类?

javascript - React js map() undefined 不是一个函数

javascript - 搜索按钮不起作用

JavaScript - 获取滚动值并将其转换为动画元素

javascript - 之后添加带有css伪元素的onclick

html - 当你在侧面有导航栏时如何使元素居中?

javascript - 如何在 iPad 上的新窗口中打开链接

javascript - ng-在页面中包含相同的部分页面两次

jquery - 预加载 css 文件到缓存中以进行主题切换

jquery - 使用 Jquery 从 HTML 数组中获取值