How do I check if an element is the last sibling?
如何检查元素是否是最后一个兄弟?
For the last cell in a row I want to perform a different action.
对于连续的最后一个单元格,我想执行不同的操作。
This doesn't work:
这不起作用:
$('td').each(function(){
var $this = $(this);
if ( $this === $this.parent().last('td') )
{
alert('123');
}
})
And neither does it if I remove .parent()
.
如果我删除.parent(),它也不会。
4 个解决方案
#1
13
Try
尝试
$('tr td:last-child').each(function(){
var $this = $(this);
alert('123');
});
#2
281
This is more elegant and worked for me:
这更优雅,对我有用:
if($(this).is(':last-child'))
{
alert('123');
}
#3
7
Here you go, but that only make sense if you want to do something to the other tds as well other wise use the last-child method described in one of the other answers.
在这里,你去,但只有你想对其他tds做一些事情才有意义。其他明智的做法是使用其他答案中描述的最后一个孩子的方法。
$('td').each(function(){
var $this = $(this);
if ($this.index() == $this.siblings().length-1)
{
alert('123');
}
})
#4
3
you can code this way.
你可以这样编码。
var $this = $(this);
if($this.index()==$this.siblings().size()-1)
{
alert("It's the last.");
}
#1
13
Try
尝试
$('tr td:last-child').each(function(){
var $this = $(this);
alert('123');
});
#2
281
This is more elegant and worked for me:
这更优雅,对我有用:
if($(this).is(':last-child'))
{
alert('123');
}
#3
7
Here you go, but that only make sense if you want to do something to the other tds as well other wise use the last-child method described in one of the other answers.
在这里,你去,但只有你想对其他tds做一些事情才有意义。其他明智的做法是使用其他答案中描述的最后一个孩子的方法。
$('td').each(function(){
var $this = $(this);
if ($this.index() == $this.siblings().length-1)
{
alert('123');
}
})
#4
3
you can code this way.
你可以这样编码。
var $this = $(this);
if($this.index()==$this.siblings().size()-1)
{
alert("It's the last.");
}