gpt4 book ai didi

javascript - 重构链接以显示/隐藏表行

转载 作者:行者123 更新时间:2023-11-29 17:32:08 25 4
gpt4 key购买 nike

我有一个表,其中的行可以被用户隐藏。它是这样实现的:

标记:

<table>
<tr>
<td>
<table style="margin-left: auto; text-align: right;">
<tr>
<td class="stats-hide">
<a href="#" onclick="hideStats();">Hide</a>
</td>
<td class="stats-show" style="display: none;">
<a href="#" onclick="showStats();">Show</a>
</td>
</tr>
</table>
</td>
</tr>
<tr class="stats-hide">
<td>
<!-- data -->
</td>
</tr>
</table>

和 jQuery 代码:

<script type="text/javascript" language="javascript">
function hideStats() {
hideControls(true, $('.stats-hide'));
hideControls(false, $('.stats-show'));
}

function showStats() {
hideControls(false, $('.stats-hide'));
hideControls(true, $('.stats-show'));
}

function hideControls(value, arr) {
$(arr).each(function () {
if (value) {
$(this).hide();
}
else {
$(this).show();
}
});
}
</script>

如何使用一个单一链接和一个(可能)CSS 类来实现相同的行为?

我的想法 - 在某处存储一个 bool 变量并切换控制相对于此变量的可见性。还有吗?

最佳答案

行隐藏在 IE 上不能正常工作吗? jQuery和浏览器是什么版本比较麻烦?

我创建了一个 example按照@Galen 的建议使用切换,它适用于 Chrome、Firefox 和 Safari。我无法访问 IE。

代码如下:

HTML

<table>
<tr>
<td>
<table class="data">
<tr>
<td class="stats-hide">
<a href="#" class="toggle-stats">Hide</a>
</td>
<td class="stats-show">
<a href="#" class="toggle-stats">Show</a>
</td>
</tr>
</table>
</td>
</tr>
<tr class="stats-hide">
<td>
Some Data
</td>
</tr>
</table>

Javascript

$(".toggle-stats").click(function() {
$(".stats-hide, .stats-show").toggle();
});

关于javascript - 重构链接以显示/隐藏表行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2844624/

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