Jquery Toggling Between Two Different Divs On Button Click

假如想象 提交于 2019-12-11 18:56:39

问题


How can I disable a jquery function using an if else statement. My problem is that I have two text boxes that open onClick, they are set to toggle, but how can I set it so that if one of the boxes is open, the other one won't open My jquery is

$(".first1").on('click', 
  function () {
    $('#suitsyou').toggle();
  });
$(".last1").on('click', 
  function () {
      $('#dealsandoffers').toggle() 
  });

It may be easier to look at my website http://www.jeremyspence.net78.net it is in the packages tab, you can see that when you click on one tab and then the other without closing the first they both open

the html is

    <div id="suitsyou" style="display:none">

    </div>

    <div id="dealsandoffers" style="display:none"> 

    </div>

Content was not included so not to put so much code in


回答1:


A simple conditional should work:

$(".first").on('click', function () {
    if (!$("#dealsandoffers:visible").length) {
        $("#suitesyou").toggle();
    }
});

Use classes if you need more flexibility than that. This also isn't particularly user friendly. Seems to me like you would want to close the other and then show the one that was currently clicked.




回答2:


Without changing to much, you could simply show the correct one and then hide the other, toggling between work work great. Although I have to admit, its not an elegant solution but if this is what you're going for, it should do the trick.

$(".first1").on('click', 
  function () {
   $('#dealsandoffers').hide()
    $('#suitsyou').show();
  });
$(".last1").on('click', 
  function () {
      $('#suitsyou').hide()
      $('#dealsandoffers').show() 
  });


来源:https://stackoverflow.com/questions/14720054/jquery-toggling-between-two-different-divs-on-button-click

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!