【问题标题】:onClick Cycle Through Multiple Sets of DIVs?onClick 循环遍历多组 DIV?
【发布时间】:2016-01-02 03:27:09
【问题描述】:

我对探索 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>

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

【问题讨论】:

  • 当用户点击this is ... message时,是否要再次显示第一条消息?

标签: jquery html css cycle


【解决方案1】:

您实际上可以使用 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();
    }
  });
});

$(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();
    }
  });
});
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="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>

【讨论】:

    【解决方案2】:
    <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);
        });
    });
    

    【讨论】:

      【解决方案3】:

      $('.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');
        }
      })
      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="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>

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-11-05
        • 2013-03-25
        • 2013-04-01
        • 2011-08-28
        相关资源
        最近更新 更多