防止带有行距的TH隐藏

时间:2012-10-31 16:36:26

标签: javascript jquery

我是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;
    }
});

1 个答案:

答案 0 :(得分:0)

隐藏其“父级”时,无法显示子元素。而是尝试隐藏<th>兄弟姐妹 - DEMO

$("#my-table tbody > tr > td").hide();