1

我是JS的新手。我正在尝试隐藏表格行,但我不知道如何保留 TH。请给个方向。

HTML:这是我试图使用 JS 隐藏行的表的代码

<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<meta charset=utf-8 />
</head>
<body>

<div>
<label for="kwd_search">Search:</label>
<input type="text" id="kwd_search"/>
<table id="my-table" border="1" style="border-collapse:collapse">
    <colgroup>
     <col class="col1" />
     <col class="col2" />
     <col class="col3" />
     <col class="col4" />
    </colgroup>
    <thead>
        <tr>
        <th>No</th>
        <th>Use case</th>
        <th>Exsample</th>
            <th>Marker</th> 
        </tr>
    </thead>
    <tbody>
        <tr>
            <th scope="rowgroup" rowspan="3"><div class="vertical-text">1</div></th>
            <td>1a</td>
            <td>1b</td>
            <td>1c</td>
        </tr>
        <tr>
        <td>1d</td>
            <td>1e</td>
            <td>1f</td>
        </tr>
        <tr>
        <td>1g</td>
            <td>1h</td>
            <td>1i</td>
        </tr>
    </tbody>
<tbody>
        <tr>
            <th scope="rowgroup" class="past-simp" rowspan="3"><div class="vertical-text">2</div></th>
            <td>2a</td>
            <td>2b</td>
            <td>2c</td>
        </tr>
        <tr>
        <td>2d</td>
            <td>2e</td>
            <td>2f</td>
        </tr>
        <tr>
        <td>2g</td>
            <td>2h</td>
            <td>2i</td>
        </tr>
    </tbody>>
</table>
</div>
</body>
</html>

JS(它是从[link]处理的):

$(document).ready(function(){
    // Write on keyup event of keyword input element
    $("#kwd_search").keyup(function(){
        // When value of the input is not blank
        if( $(this).val() !== "")
        {
            // Show only matching TR, hide rest of them
            $("#my-table tbody>tr").hide();
            $("#my-table td:contains-ci('" + $(this).val() + "')").parent("tr").show();
        }
        else
        {
            // When there is no input or clean again, show everything back
            $("#my-table tbody>tr").show();
        }
    });
});
// jQuery expression for case-insensitive filter
$.extend($.expr[":"], 
{
    "contains-ci": function(elem, i, match, array) 
    {
        return (elem.textContent || elem.innerText || $(elem).text() || "").toLowerCase().indexOf((match[3] || "").toLowerCase()) >= 0;
    }
});
4

1 回答 1

0

隐藏其父元素时不能显示子元素。而是尝试隐藏<th>兄弟姐妹 - DEMO

$("#my-table tbody > tr > td").hide();
于 2012-10-31T16:44:37.990 回答