gpt4 book ai didi

jquery - 在 jquery 中切换表行隐藏/显示

转载 作者:行者123 更新时间:2023-12-01 07:47:12 27 4
gpt4 key购买 nike

我有一个表,我希望有一个 onClick 按钮来运行切换()函数

目前,我可以隐藏 M 值,但不能将它们恢复,

举例来说,这就是表格的样子

+-------+--------+
| Name | Gender |
+-------+--------+
| James | M |
+-------+--------+
| Gemma | F |
+-------+--------+
| John | F |
+-------+--------+

这是切换功能

function toggle() {
var searchString = 'M';
$("#t01 tr td:contains('" + searchString + "')").each(function() {
if ($(this).text() == searchString) {
$(this).parent().hide();
}
});
}

最佳答案

tr进入并检查内部会更容易,如下所示:-

function toggle(gender) {
$('#t01 tbody tr').each(function() {
$(this).toggle($('td:eq(1):contains(' + gender + ')', this).length > 0);
})
}

function show() {
$('#t01 tbody tr').show();
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<table id="t01">
<thead>
<tr>
<th>Name</th>
<th>Gender</th>
</tr>
</thead>
<tbody>
<tr>
<td>James</td>
<td>M</td>
</tr>
<tr>
<td>Gemma</td>
<td>F</td>
</tr>
<tr>
<td>John</td>
<td>F</td>
</tr>
</tbody>
</table>

<button onclick="toggle('M')">Male</button>
<button onclick="toggle('F')">Female</button>
<button onclick="show()">All</button>

关于jquery - 在 jquery 中切换表行隐藏/显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35224955/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com