你怎么知道当前元素不是第一个孩子?
它应该与 $(this)
一起使用,例如:
$("li").click(function(e) {
if (/* $(this) is not the first-child */)
{
/* do something */
}
});
原文由 James 发布,翻译遵循 CC BY-SA 4.0 许可协议
你怎么知道当前元素不是第一个孩子?
它应该与 $(this)
一起使用,例如:
$("li").click(function(e) {
if (/* $(this) is not the first-child */)
{
/* do something */
}
});
原文由 James 发布,翻译遵循 CC BY-SA 4.0 许可协议
您可以简单地询问其 .index()
位置(相对于其兄弟姐妹)。
$(this).index(); // returns a zero based index position
原文由 user113716 发布,翻译遵循 CC BY-SA 2.5 许可协议
6 回答936 阅读✓ 已解决
2 回答1.4k 阅读✓ 已解决
2 回答857 阅读✓ 已解决
1 回答1.1k 阅读✓ 已解决
1 回答856 阅读✓ 已解决
2 回答766 阅读
1 回答744 阅读✓ 已解决
您可以这样做只是为了测试一个元素是否是第一个子元素:
$(this).is(':first-child')
。其他选择器,如:last-child
也可以。