Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 如何确定next()何时到达末尾,然后转到第一项_Jquery_Next - Fatal编程技术网

Jquery 如何确定next()何时到达末尾,然后转到第一项

Jquery 如何确定next()何时到达末尾,然后转到第一项,jquery,next,Jquery,Next,我正在使用next()函数显示一系列元素。一旦我到达终点,我想进入第一个元素。有什么想法吗 代码如下: //Prev / Next Click $('.nextSingle').click( function() { //Get the height of the next element var thisHeight = $(this).parent().parent().parent().next('.newsSingle').attr('rel'); //Hide

我正在使用next()函数显示一系列元素。一旦我到达终点,我想进入第一个元素。有什么想法吗

代码如下:

//Prev / Next Click
$('.nextSingle').click( function() {
    //Get the height of the next element
    var thisHeight = $(this).parent().parent().parent().next('.newsSingle').attr('rel');
    //Hide the current element
    $(this).parent().parent().parent()
        .animate({
            paddingBottom:'0px',
            top:'48px',
            height: '491px'
        }, 300) 
        //Get the next element and slide it in      
        .next('.newsSingle')
        .animate({
            top:'539px',
            height: thisHeight,
            paddingBottom:'100px'
        }, 300);
});
基本上,我需要一个“if”语句,它说“如果没有剩余的‘next’元素,那么就找到第一个”


谢谢!

根据jquery文档,空jquery对象将返回.0长度

所以你需要做的是在打电话时检查退货。下一步,然后打电话:first

通过检查
长度属性,提前确定
.next()

$('.nextSingle').click( function() {
       // Cache the ancestor
    var $ancestor = $(this).parent().parent().parent();
       // Get the next .newsSingle
    var $next = $ancestor.next('.newsSingle');
       // If there wasn't a next one, go back to the first.
    if( $next.length == 0 ) {
        $next = $ancestor.prevAll('.newsSingle').last();;
    }

    //Get the height of the next element
    var thisHeight = $next.attr('rel');

    //Hide the current element
    $ancestor.animate({
            paddingBottom:'0px',
            top:'48px',
            height: '491px'
        }, 300);

        //Get the next element and slide it in      
    $next.animate({
            top:'539px',
            height: thisHeight,
            paddingBottom:'100px'
        }, 300);
});
顺便说一下,您可以将
.parent().parent().parent()
替换为
.closest('.newsingle')
(如果标记允许的话)


编辑:我更正了
此高度
以使用我们引用的
$next
元素。

作为有用的参考,以下是您可以编写并包括的函数:

$.fn.nextOrFirst = function(selector)
{
  var next = this.next(selector);
  return (next.length) ? next : this.prevAll(selector).last();
};

$.fn.prevOrLast = function(selector)
{
  var prev = this.prev(selector);
  return (prev.length) ? prev : this.nextAll(selector).last();
};
而不是:

var $next = $ancestor.next('.newsSingle');
   // If there wasn't a next one, go back to the first.
if( $next.length == 0 ) {
    $next = $ancestor.prevAll('.newsSingle').last();;
}
它将是:

$next = $ancestor.nextOrFirst('.newsSingle');

参考:

您可以使用这些函数查看当前项是否为第一个/最后一个子项

jQuery.fn.isFirst = function() { return (this[0] === this.parent().children().first()[0]); };
jQuery.fn.isLast = function() { return (this[0] === this.parent().children().last()[0]); };

if($ancestor.isLast())
{
    // ...
}
else
{
    // ...
}

thx patrick!看起来很有希望,但我得到了“Error:$祖先.prevAll(“.newsSingle”).last不是一个函数”@j-man86-您使用的是什么版本的jQuery?
.last()
方法是在jQuery 1.4中添加的。@j-man86-正准备进行编辑。我只留下一条注释。如果您需要较旧版本的jQuery,则可以使用
而不是
prevAll().last()
。prevAll(':last')
。效果很好。它可以是
这个。兄弟(选择器)。first()
last()
If($).element“).next().length>0{}