gpt4 book ai didi

jquery - 在 jQuery 中仅获取连续的每个 td 的第一个子元素

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

例如我有这种表

<table>
<tr>
<td><input textbox text="test1"> <!-- FIND only this element -->
<div></div>
</td>
<td>test2 <!-- FIND only this text and disregard checkbox input-->
<checkbox>
</td>
</tr>
</table>

我正在创建一个 .each() 函数,它只会获取每行每个 td 的每个第一个元素的值。

我已经尝试过了$(this).find('tr td:first-child').each() 函数,但我知道这只会获取每行的第一个 td ,它不会遍历我想要的每个td

最佳答案

<table border="1" class="myTable">
<tr>
<td>
<span>First</span>
<span>Second</span>
<span>Third</span>
</td>
<td>
<span>First</span>
<span>Second</span>
<span>Third</span>
</td>
</tr>
</table>

//jQuery code
$('.myTable tr td').each(function() {

//First child of td
$(this).children(':first').css('color', 'red');

});

这是working sample

如果您只需要第一个元素的文本,那么您可以使用 text() 来实现功能

$('.myTable tr td').each(function() {

//First child of td
var firstChild = $(this).children(':first');
alert(firstChild.text());

});

关于jquery - 在 jQuery 中仅获取连续的每个 td 的第一个子元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57634696/

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