简体   繁体   English

jQuery .next()div显示所有同级而不是一个

[英]jQuery .next() div showing all siblings instead of just one

I'm testing a pagination type where the buttons are on a separate div from the target. 我正在测试分页类型,其中按钮位于与目标不同的div上。

When I try to toggle the next div of the body, all of them are toggling, instead of just one. 当我尝试切换身体的下一个div时,它们都在切换,而不仅仅是一个。

 $('#next_q').on('click', function(){ $('.question-item').hide().next('.question-item').show(); }) 
 .question-item:not(:first-child){display: none;} 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="box-body"> <div class="question-item">Q1</div> <div class="question-item">Q2</div> <div class="question-item">Q3</div> </div> <div class="box-footer"> <button>Prev</button> <button id="next_q">Next</button> </div> 

use :visible to select the question-item that is currently visible 使用:visible选择question-item当前可见

$('#next_q').on('click', function(){
    $('.question-item:visible').hide().next('.question-item').show();
})

also if you want it not to "skip" past the last element, use 另外,如果您不希望“跳过”最后一个元素,请使用

if ($('.question-item:visible').prev('.question-item').length)

 $('#next_q').on('click', function() { if ($('.question-item:visible').next('.question-item').length) $('.question-item:visible').hide().next('.question-item').show(); }) $('#prev_q').on('click', function() { if ($('.question-item:visible').prev('.question-item').length) $('.question-item:visible').hide().prev('.question-item').show(); }) 
 .question-item:not(:first-child) { display: none; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="box-body"> <div class="question-item">Q1</div> <div class="question-item">Q2</div> <div class="question-item">Q3</div> </div> <div class="box-footer"> <button id="prev_q">Prev</button> <button id="next_q">Next</button> </div> 

Check the current visible .question-item and hide it and then show the next .question-item . 检查当前可见的.question-item并将其隐藏,然后显示下一个.question-item I also added the not last-child selector since I thought that you might want to stop at the last question. 我还添加了not last-child选择器,因为我认为您可能想停在最后一个问题。

  $('#next_q').on('click', function(){ $('.question-item:visible:not(:last-child)').hide().next('.question-item').show(); }); $('#prev_q').on('click', function(){ $('.question-item:visible:not(:first-child)').hide().prev('.question-item').show(); }); 
 .question-item:not(:first-child){display: none;} 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="box-body"> <div class="question-item">Q1</div> <div class="question-item">Q2</div> <div class="question-item">Q3</div> </div> <div class="box-footer"> <button id="prev_q">Prev</button> <button id="next_q">Next</button> </div> 

You can use .data() to store an integer reflecting an index of the .question-item elements .length , use ++ and % operators to cycle the indexes passed to .eq() from current index or 0 through .question-items .length 您可以使用.data()存储反映.question-item元素.length索引的整数,使用++%运算符循环从当前索引或0.question-items传递给.eq()索引.length

 $('#next_q') .data({ "index": 0, items: $(".question-item") }) .on("click", function() { $(this).data().items .hide() .eq(++$(this).data().index % $(this).data().items.length) .show(); }) 
 .question-item:not(:first-child) { display: none; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"> </script> <div class="box-body"> <div class="question-item">Q1</div> <div class="question-item">Q2</div> <div class="question-item">Q3</div> </div> <div class="box-footer"> <button>Prev</button> <button id="next_q">Next</button> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM