Using JQuery how to show and hide different div's onClick event

前端 未结 12 745
死守一世寂寞
死守一世寂寞 2021-01-03 03:34

I would like to show a div based on the Onclick event of an link.

First Click - Show div1
Second Click - Hide remaining div\'s and Show div2
Third Click

12条回答
  •  余生分开走
    2021-01-03 04:19

    My solution is a little different - I'd do it dependant on the state of the divs at the current time (on click). See below for what I mean by this.

    $(document).ready(function() {
        $("#toggle_value").click(function(){
           if ($("#div1).is(':visible')) { // Second click
                // Hide all divs and show d2
                $("#div1").hide();
                $("#div2").show("fast");
                $("#div3").hide();
                $("#div4").hide();
           } else if ($("#div2").is(':visible')) { // Third click
                // follow above example for hiding all and showing div3
           } else if ($("#div3").is(':visible')) { // Fouth click
                // follow above example for hiding all and showing div1
           } else { // first click
                // All divs should be hidden first. Show div1 only.
                $("#div1").show("fast");
           }
        });
    });
    

    Just to warn you - I have not tested this code :) Based upon the following for determining visibility: http://docs.jquery.com/Frequently_Asked_Questions#How_do_I_determine_the_state_of_a_toggled_element.3F

    Hope it helps

提交回复
热议问题