jQuery get elements without display=“none”

邮差的信 提交于 2021-01-20 17:51:26

问题


How can I get all tbody > tr that doesn't have this style attribute: display: none?


回答1:


$("tbody > tr:visible")

Should do it, by using the :visible selector.




回答2:


The accepted answer works, and is very useful, but not technically what the OP directly asked.

I'm splitting hairs, admittedly, but I needed to find only tr elements which literally did not contain display: none within their style attribute, because the parent element might be hidden, thus returning no elements.

So I wrote the following:

var $trWithoutDisplayNone = $('tbody > tr:not([style*="display: none"])');

Update:

The original code will select an array of all trs on a page with no style attribute containing "display: none".

A more efficient and specific way would be to target the table itself.

HTML:

<table id="tableID">
    <tr>
        <td>1</td>
        <td>2</td>
        <td>3</td>
        <td>4</td>
    </tr>
    <tr style="background: grey; display: none;">
        <td>5</td>
        <td>6</td>
        <td>7</td>
        <td>8</td>
    </tr>
    <tr>
        <td>9</td>
        <td>10</td>
        <td>11</td>
        <td>12</td>
    </tr>
</table>

JavaScript/jQuery:

<script>
    function getDisplayedRows($targetTable) {
            return $targetTable.find('tr:not([style*="display: none"])');
    }

    $(function() { //shortcut for document ready
        var $table = $("#tableID"), //get table by selector
            $visibleRows = getDisplayedRows($table); //run function to get rows without display: none
    });
</script>


来源:https://stackoverflow.com/questions/5767334/jquery-get-elements-without-display-none

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