[英]jQuery each: If element is first-child else
So I'm trying to run an each loop in jQuery that has an if statement to determine if the element it's on is the first-child of it's parent and also have the else statement following (this seems to be the difficult part) to have other code run for those. 因此,我试图在jQuery中运行每个循环,该循环具有if语句来确定其所在元素是否是其父元素的第一个子元素,并且后面还要包含else语句(这似乎是困难的部分)其他代码则针对这些代码运行。
Everything I've tried and found only seems to work without the if and else.. 我尝试过并发现的所有内容似乎都只能在没有if和else的情况下工作。
Any thoughts? 有什么想法吗?
Given the following example: 给出以下示例:
<table>
<tr>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
</tr>
</table>
there are a couple of ways to do it 有几种方法可以做到这一点
$("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
});
Or 要么
$("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
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.