jquery上有循环next()吗? [英] Is there a circular next() on jquery?

查看:64
本文介绍了jquery上有循环next()吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码:

<div class="container">
    <div class="prova">1</div>
    <div class="prova">2</div>
    <div class="prova">3</div>
</div>

我想每500分钟获得每个div的内容。当我到达第3个位置时,返回第一个位置,依此类推。圆形 next()

I want to get every 500ms the content of each div. When I reach the 3rd position, return to the first one, and so on. A circular next().

尝试:

var primoElem = $('.prova').first();
setInterval(function () {
    console.log(primoElem.html());
    primoElem = primoElem.next();
}, 500);

但我只得到3个结果,然后停止。

but I get only 3 results, then it stops.

推荐答案

当jQuery函数无法正常工作时,您可以随时通过保存旧函数并覆盖它来更改它。或者,如果您不想更改jquery行为,也可以创建自己的函数。例如,对于重写,在jquery加载后将其添加到代码中:

When a jQuery function doesnt work as you want, you can always change it for your need by saving the old function and override it. Or you can also create your own function if you dont want to change jquery behavior. As example, for overiding, add this to your code after jquery load :

$.fn.oldNext = $.fn.next;

$.fn.next = function(selector){
    var selector = selector || '';
    return this.oldNext(selector).length ? this.oldNext(selector) : this.siblings(selector).addBack(selector).first();
}

然后你的代码就可以了。

Then your code will work.

如果您不想覆盖,只需更改名称:

If you dont want to override, just change the name :

$.fn.loopNext = function(selector){
    var selector = selector || '';
    return this.next(selector).length ? this.next(selector) : this.siblings(selector).addBack(selector).first();
}

然后将其称为:

primoElem.loopNext();

小提琴: http://jsfiddle.net/EcnSH/

这篇关于jquery上有循环next()吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆