onClick循环通过多组DIV?

时间:2016-01-02 03:27:09

标签: jquery html css cycle

我对探索JS非常陌生,但我正在摆弄this thread中的代码来提出这个问题:jsfiddle



(function(){

var $hints = $('.hint');
var i = 0;

$('.hint').on('click', function(){
i = (i + 1) % $hints.length;
$hints.hide().eq(i).show();
});


})();

div.hint.hidden, div.hint2.hidden, div.hint3.hidden {display:none;}
div.hint-box, div.hint2-box, div.hint3-box{
    position:relative;
    padding:20px 40px 20px;
    background:grey;
    border:1px solid #CCCCCC;
    color:white;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="hint-box">
    <div class="hint">This is the first example...</div>
    <div class="hint hidden">This is the second example...</div>
    <div class="hint hidden">This is the third example...</div>    
    </div>
    <div class="hint2-box">
    <div class="hint2">This is the first example...</div>
    <div class="hint2 hidden">This is the second example...</div>
    <div class="hint2 hidden">This is the third example...</div>    
    </div>
    <div class="hint3-box">
    <div class="hint3">This is the first example...</div>
    <div class="hint3 hidden">This is the second example...</div>
    <div class="hint3 hidden">This is the third example...</div>    
    </div>
&#13;
&#13;
&#13;

第一个div集是循环我想要它的方式。我认为有一种方法可以通过复制JS并替换类(jsfiddle here)来使其他两个工作。有没有办法编写JS,我不需要为每个循环类组编写新的JS?

3 个答案:

答案 0 :(得分:0)

<div class="hint-box">
    <div class="hint">This is the first example...</div>
    <div class="hint hidden">This is the second example...</div>
    <div class="hint hidden">This is the third example...</div>    
</div>
<div class="hint-box">
    <div class="hint">This is the first example...</div>
    <div class="hint hidden">This is the second example...</div>
    <div class="hint hidden">This is the third example...</div>    
</div>
<div class="hint-box">
    <div class="hint">This is the first example...</div>
    <div class="hint hidden">This is the second example...</div>
    <div class="hint hidden">This is the third example...</div>    
</div>


jQuery(function($){
    var $hintBoxes = $('.hint-box');

    //initialize data element to be 0 (i)
    $hintBoxes.data('displayedHint', 0);

    $('.hint').on('click', function(){
        var $this = $(this),
            $parent = $(this).parent(),
            $hints = $parent.find('.hint'),
            i = ($parent.data('displayedHint') + 1) % $hints.length;

        $hints.hide().eq(i).show();
        //update which one is displayed
        $parent.data('displayedHint', i);
    });
});

答案 1 :(得分:0)

您实际上可以使用jQuery DOM遍历方法轻松实现此目的:

$(function(){
  $('.hint').on('click', function(){
    var $hint = $(this);
    //if next element is present
    if($hint.next().length > 0){
      $hint.next().show().siblings().hide();
    }
    //if you do not want to show the first message again then just delete the else part written below
    else{
      //if next element is not present then startover from first
      $hint.siblings().first().show().siblings().hide();
    }
  });
});

&#13;
&#13;
$(function(){
  $('.hint').on('click', function(){
    var $hint = $(this);
    //if next element is present
    if($hint.next().length > 0){
      $hint.next().show().siblings().hide();
    }
    else{
      //if next element is not present then startover from first
      $hint.siblings().first().show().siblings().hide();
    }
  });
});
&#13;
div.hint.hidden, div.hint2.hidden, div.hint3.hidden {display:none;}
div.hint-box, div.hint2-box, div.hint3-box{
    position:relative;
    padding:20px 40px 20px;
    background:grey;
    border:1px solid #CCCCCC;
    color:white;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="hint-box">
    <div class="hint">This is the first example...</div>
    <div class="hint hidden">This is the second example...</div>
    <div class="hint hidden">This is the third example...</div>    
    </div>
    <div class="hint-box">
    <div class="hint">This is the first example...</div>
    <div class="hint hidden">This is the second example...</div>
    <div class="hint hidden">This is the third example...</div>    
    </div>
    <div class="hint-box">
    <div class="hint">This is the first example...</div>
    <div class="hint hidden">This is the second example...</div>
    <div class="hint hidden">This is the third example...</div>    
    </div>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

&#13;
&#13;
$('.hint').on('click', function() {
  // Get the number of hints
  var hintLength = $(this).next().length;

  if (hintLength === 0) { 
    //if were at the last hint, hide this hint and show the first
    $(this).addClass('hidden');
    $(this).siblings(':first').removeClass('hidden');
  } else { 
    // Otherwise, hide this hint and show the next
    $(this).addClass('hidden');
    $(this).next().removeClass('hidden');
  }
})
&#13;
div.hint.hidden,
div.hint2.hidden,
div.hint3.hidden {
  display: none;
}

div.hint-box,
div.hint2-box,
div.hint3-box {
  position: relative;
  padding: 20px 40px 20px;
  background: grey;
  border: 1px solid #CCCCCC;
  color: white;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="hint-box">
  <div class="hint">This is the first example...</div>
  <div class="hint hidden">This is the second example...</div>
  <div class="hint hidden">This is the third example...</div>
</div>
<div class="hint-box">
  <div class="hint">This is the first example...</div>
  <div class="hint hidden">This is the second example...</div>
  <div class="hint hidden">This is the third example...</div>
</div>
<div class="hint-box">
  <div class="hint">This is the first example...</div>
  <div class="hint hidden">This is the second example...</div>
  <div class="hint hidden">This is the third example...</div>
</div>
&#13;
&#13;
&#13;