gpt4 book ai didi

jquery - 与上面相同时隐藏内容

转载 作者:行者123 更新时间:2023-12-01 08:35:53 24 4
gpt4 key购买 nike

给定一个像这样的简单表格:

当文本与上行中的文本相同时,我想隐藏该文本。

我已经尝试过这个...但我对 JQuery 真的很陌生。

$(document).ready(function() {
let previous = "*";
$('.nodup').each(function(i, obj) {
if (obj.html() == previous) {
$(obj).hide();
} else {
previous = obj.html(); // "reset" the previous value?
}
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
<tr>
<td class="nodup">one</td> show this td
<td>5</td>
<td>7</td>
<td>3</td>
</tr>
<tr>
<td class="nodup">one</td> hide this td
<td>6</td>
<td>9</td>
<td>1</td>
</tr>
<tr>
<td class="nodup">one
<td> hide this td
<td>3</td>
<td>8</td>
<td>3</td>
</tr>
<tr>
<td class="nodup">two</td> show this td
<td>7</td>
<td>3</td>
<td>1</td>
</tr>
</table>

所以最终会是这样的......

one    5    7    3
6 9 1
3 8 3
two 7 3 1

等等。

最佳答案

一种选择是使用 filter 循环遍历 $(".nodup") 元素。如果文本与 temp 变量相同(意味着与前一个相同),则返回 true。为 temp 分配新值,否则返回 false。

html 设置为空字符串并且不删除它。

var temp = null;
$(".nodup").filter(function(i, o) {
var text = $(this).text().trim();
if (text === temp) return true;

temp = text;
return false;
}).html("");
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
<tr>
<td class="nodup">one</td>
<td>5</td>
<td>7</td>
<td>3</td>
</tr>
<tr>
<td class="nodup">one</td>
<td>6</td>
<td>9</td>
<td>1</td>
</tr>
<tr>
<td class="nodup">one</td>
<td>3</td>
<td>8</td>
<td>3</td>
</tr>
<tr>
<td class="nodup">two</td>
<td>7</td>
<td>3</td>
<td>1</td>
</tr>
</table>

关于jquery - 与上面<tr>相同时隐藏<td>内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55748405/

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