Javascript Jquery-在循环中的3个div上运行css规则,每N秒一个

Javascript Jquery-在循环中的3个div上运行css规则,每N秒一个,javascript,jquery,html,css,Javascript,Jquery,Html,Css,我有3个HTML div,例如: idArray=["one", "two", "three"]; HTML <div id="one" class="hide">Text one</div> <div id="two" >Text two</div> <div id="three" class="hide">Text three</div> 下一个N秒:再次执行“前N秒”操作,然后再次循环下两个 我尝试过设置间隔,但不

我有3个HTML div,例如:

idArray=["one", "two", "three"];
HTML

<div id="one" class="hide">Text one</div>
<div id="two" >Text two</div>
<div id="three" class="hide">Text three</div>
下一个N秒:再次执行“前N秒”操作,然后再次循环下两个

我尝试过设置间隔,但不确定如何实现这一点,因为它每次都会经过一个或几个动作

我曾考虑过检查css是否可以看到div,并以此来执行操作,但我正在寻找一种方法来执行此操作,而不需要依靠div的css每N秒执行一次操作

div可能位于页面中的任何位置,因此无法使用“next”到达下一个div,我需要从具有divs ID的数组中运行它们。例如:

idArray=["one", "two", "three"];

您的代码有点误导,因为您提到的选择器具有id,而不是预期的类

相反,您可以访问所有div,并使用
setInterval
addClass
removeClass
hide
方法的组合来生成一些内容

var $curr =  $('div').first();
// Hide all the divs
$('div').addClass('hide');
// Display the first div
$curr.removeClass('hide'); 

var interval = setInterval(function() {
    // Hide all the divs
    $('div').addClass('hide');
    if($curr) {
        $curr = $curr.next('div');
    }

    if($curr && $curr.length === 0) {
         $curr = $('div').first();   
    }
    $curr.removeClass('hide');                       
}, 500);

更新

页面上任何位置存在的元素。为了让它工作,我将在所有必须显示和隐藏的元素上有一个公共类

var $curr = $('.toggle').first();
// Hide all the divs
$('.toggle').addClass('hide');
// Display the first div
$curr.removeClass('hide');

var interval = setInterval(function () {
    // Hide all the divs
    $('.toggle').addClass('hide');
    var len = $('.toggle').length,
        found = false;

    $(".toggle").each(function(index) {
        // If the curr element is the 
        // element in iteration
        // select the next indexed element..
        if($(this).is($curr) && !found) {
             var nextIndex = index + 1;
            if(nextIndex >= len) {
                $curr = $('.toggle').first();
            } else {
                $curr = $('.toggle').eq(nextIndex);
                found = true;
            }
         }
    });
    $curr.removeClass('hide');
}, 500);

首先,您使用的是一个
类选择器
,但相同的div却有id。该示例非常有效。问题是div可能在页面中的任何位置,所以在本例中“next”将起作用。在现在的问题中添加了这一点,很抱歉之前没有提及。@batz如果是这样的话,我已经更新了代码。检查无论在何处提供元素都应该有效的帖子,它被赋予了一个公共类。很高兴能够提供帮助:)