如果一个div是第一个孩子的jQuery测试?

red*_*ory 3 jquery conditional

如果图像是一组图像的第一个或最后一个,如何测试?

我正在尝试使上一个和下一个按钮动画进出,这取决于我是否在一系列图像的末尾使用以下代码:

var $current = jQuery("img .active");
var $next = $current.next();

if ($next != jQuery("img:first-child")
{
 // show next item and the previous button
 // seems to work?

} else if ($next == jQuery("img:last-child")
{
 // hide the next button since we're at the end
 // doesn't seem to work??
}
Run Code Online (Sandbox Code Playgroud)

Dav*_*ang 8

你想检查img的索引:

var $current = jQuery("#img .active"),
    index = $current.index();

if (index === 0) {
    // This is the first
} else if (index === jQuery("#img").children().length - 1) {
    // This is the last
}
Run Code Online (Sandbox Code Playgroud)