gpt4 book ai didi

javascript - jQuery 每个 : If element is first-child else

转载 作者:行者123 更新时间:2023-11-30 07:38:52 25 4
gpt4 key购买 nike

所以我试图在 jQuery 中运行一个 each 循环,它有一个 if 语句来确定它所在的元素是否是它的父元素的第一个子元素,并且后面还有 else 语句(这似乎是困难的部分) 为那些运行其他代码。

我尝试和发现的所有东西似乎在没有 if 和 else 的情况下都有效..

有什么想法吗?

最佳答案

给定以下示例:

<table>
<tr>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
</tr>
</table>

有几种方法可以做到这一点

$("table tr td:first-child").each(function() {
//perform actions on all of the first TD elements
});
$("table tr td:not(:first-child)").each(function() {
//perform actions on all of the other TD elements
});

或者

$("table tr td").each(function() {
var td = $(this);
if (td.is(":first-child")) {
//perform actions on all of the first TD elements
}
else {
//perform actions on all of the other TD elements
}
});

关于javascript - jQuery 每个 : If element is first-child else,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22870039/

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