简体   繁体   English

Javascript通过单击相同的按钮逐个切换多个div

[英]Javascript toggle multiple div one by one by clicking same button

So I have this code in my form, and I want to toggle each divs one by one by clicking the same button repeatedly. 所以我在我的表单中有这个代码,我想通过重复单击相同的按钮逐个切换每个div。 But when I click the button, everything shows up, is there a way to toggle it one by one by clicking the same button? 但是当我点击按钮时,一切都显示出来,有没有办法通过点击相同的按钮逐个切换它?

<div id="msg2" class="hidemsg">
    <label for="commentcont" class="control-label">Message 2:</label>
    <input type="text" class="form-control" name="comment2" placeholder="New message." />
</div>
<div id="msg2" class="hidemsg">
    <label for="commentcont" class="control-label">Message 3:</label>
    <input type="text" class="form-control" name="comment3" placeholder="New message." />
</div>
<div id="msg2" class="hidemsg">
    <label for="commentcont" class="control-label">Message 4:</label>
    <input type="text" class="form-control" name="comment4" placeholder="New message." />
</div>
<div id="msg2" class="hidemsg">
    <label for="commentcont" class="control-label">Message 5:</label>
    <input type="text" class="form-control" name="comment5" placeholder="New message." />
</div>

Here's the button that toggles it; 这是切换它的按钮;

<a href="#" type="button" class="btn btn-default" onclick="toggler(\'msg2\');">New Message</a>

The javascript code for toggling it; 用于切换它的javascript代码;

function toggler(divId) {
    $('#' + divId).toggle();
}

I'm just a beginner with js and I want to toggle the inputs one by one by clicking the same button. 我只是js的初学者,我想通过点击相同的按钮逐个切换输入。 So its like when i click the new message: Message 2 will appear then when I click it again, Message 3 will appear so on and so forth.! 所以当我点击新消息时就像它一样: Message 2 will appear然后当我再次点击它时, Message 3 will appear等等。

I'm not sure but try below mentioned code 我不确定,但尝试下面提到的code

Use below mentioned HTML code: 使用下面提到的HTML代码:

<a href="#" type="button" class="btn btn-default" onclick="toggler();">New Message</a>

Use below mentioned javascript code: 使用下面提到的javascript代码:

var x=1;
function toggler() {
   var hidemsgLength=$('.hidemsg').length; 
   if(x<hidemsgLength){
      $('.hidemsg').eq(x).show();
      x++;
   }
}

Now coming to the css Part: 现在来到css部分:

.hidemsg{display:none;}
.hidemsg:first-child{display:block;}

Firstly it might be better to use unique IDs on div. 首先,在div上使用唯一ID可能更好。

For multiple elements, prefer class. 对于多个元素,更喜欢上课。

Teran's solution is : Teran的解决方案是:

  • on load, all divs are displayed 在加载时,显示所有div
  • on first click, first div is hidden 在第一次单击时,第一个div被隐藏
  • on second click, second div is hidden 在第二次单击时,隐藏第二个div
  • ... ...

It means you can use $(".my_class:visible").first().toggle(); 这意味着你可以使用$(".my_class:visible").first().toggle();

First of all, an ID can only be used once . 首先, ID只能使用一次

What you can do, if I understand your question correctly, is first hide all messages and display the first. 你能做什么,如果我理解你的问题,首先要隐藏所有信息并显示第一个。

Then, when the user clicks the button, display the second, then the 3th, etc. I am not fond of an inline click handler, so i added an eventListener in JS. 然后,当用户单击按钮时,显示第二个,然后显示第3个,等等。我不喜欢内联点击处理程序,所以我在JS中添加了一个eventListener

// reference to button
var btn = document.querySelector('.js-button');

// which message to display
var index = 1;

// total number of messages
var numMessages = $('.hidemsg').length;

// hide 'm all
$('.hidemsg').hide();

// and toggle the first
toggler();

// when user clicks button, toggle the next one
btn.addEventListener('click', toggler);

function toggler() {
    $('.hidemsg').hide();   
    $('#msg' + index).show();

        // go to 1 again when the end is reached
    index = (index + 1 <= numMessages) ? index + 1 : 1;
}

https://jsfiddle.net/j32yjsaw/1/ https://jsfiddle.net/j32yjsaw/1/

I used a lot of comments to help you see exactly what was going on in the function. 我使用了很多注释来帮助您准确了解函数中发生了什么。 But basically, I'm keeping track of which div should be displayed by using a data- attribute in the toggle anchor. 但基本上,我通过在toggle锚点中使用data-属性来跟踪应该显示哪个div

 //Set up the click event listener $('#toggler').on('click', function () { //Get the 'count' data property from the anchor tag var counter = $(this).data('count'); //assign that to be the ID of the message you want to display var nextMessage = '#msg' + counter; $(nextMessage).show(); //Show that Message counter += 1; //increment counter (for next click) $(this).data('count', counter); //assign new counter var to #toggler }); 
 .hidemsg { display: none; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <a href="#" data-count="2" id="toggler" type="button" class="btn btn-default">New Message</a> <div id="msg2" class="hidemsg"> <label for="commentcont" class="control-label">Message 2:</label> <input type="text" class="form-control" name="comment2" placeholder="New message." /> </div> <div id="msg3" class="hidemsg"> <label for="commentcont" class="control-label">Message 3:</label> <input type="text" class="form-control" name="comment3" placeholder="New message." /> </div> <div id="msg4" class="hidemsg"> <label for="commentcont" class="control-label">Message 4:</label> <input type="text" class="form-control" name="comment4" placeholder="New message." /> </div> <div id="msg5" class="hidemsg"> <label for="commentcont" class="control-label">Message 5:</label> <input type="text" class="form-control" name="comment5" placeholder="New message." /> </div> 

The problem is that all your div's have the same id. 问题是你所有的div都有相同的id。 If you change the id's so that they are unique, with and increasing trailing value (msg1, msg2, ...), the function can be changed to something like this: 如果更改id以使它们是唯一的,使用并增加尾随值(msg1,msg2,...),则可以将该函数更改为如下所示:

    var counter = 1;
    function toggler(divId) {
         $('#' + divId + counter).toggle();
    counter++;
    }

I would like to keep it short and sweet, where you think about clicked element and the next element. 我想保持简短和甜蜜,你想到点击元素和下一个元素。 Let me know if that works for you. 如果这对您有用,请告诉我。 Good luck! 祝好运!

 $(function() { $('.js-button').click(function() { if ($('div.active').next().hasClass('hidemsg')) { $('div.active').next('div').addClass('active').end().removeClass('active'); }else{ alert('Sorry, there is no next message'); } }); }); 
 .hidemsg { display: none; } .hidemsg.active { display: block; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="msg1" class="hidemsg active"> <label for="commentcont" class="control-label">Message 2:</label> <input type="text" class="form-control" name="comment2" placeholder="New message." /> </div> <div id="msg2" class="hidemsg"> <label for="commentcont" class="control-label">Message 3:</label> <input type="text" class="form-control" name="comment3" placeholder="New message." /> </div> <div id="msg3" class="hidemsg"> <label for="commentcont" class="control-label">Message 4:</label> <input type="text" class="form-control" name="comment4" placeholder="New message." /> </div> <div id="msg4" class="hidemsg"> <label for="commentcont" class="control-label">Message 5:</label> <input type="text" class="form-control" name="comment5" placeholder="New message." /> </div> <a href="#" type="button" class="btn btn-default js-button">New Message</a> 

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

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