我有通过选择重复的 thead 元素来删除重复行的代码。它删除了最后一个 thead 元素,但是我将如何选择未删除的 thead 元素中的元素并执行以下操作:
- 选择第一个
<span>
元素并隐藏它 - 选择第二个
<span>
元素并显示它
到目前为止,我知道我可以使用 find() 方法找到第一个跨度,我知道我可以使用 nth-of-type(2) 找到它的兄弟。
我不想给 td 或 span 元素任何进一步的类名或 ID 名。并且请不要使用 hidspan 类名来显示。
CSS
.hiddenspan {
display: none;
}
HTML
<table id="test">
<thead class="test className">
<tr>
<td>test1<td>
</tr>
<tr>
<td>test2</td>
<td><span>some text</span>
<span class="hiddenspan">some other text</span>
</td>
</tr>
</thead>
<thead class="test className">
<tr>
<td>test1<td>
</tr>
<tr>
<td>test2<td>
</tr>
</thead>
</table>
我想要这个结果
<table id="test">
<thead class="test className">
<tr>
<td>test1<td>
</tr>
<tr>
<td>test2</td>
<td><span>some other text</span></td>
</tr>
</thead>
</table>
J查询:
var $thead = $('thead.test.className:contains(test1)');
if ($thead.length > 1) {
$thead.not(':first').remove()
}
最佳答案
我认为最好的方法
$("#test span:nth-child(1)").hide();
$("#test span:nth-child(2)").show();
关于jquery - 如何在树中查找元素和 sibling ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39024477/