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

前端 未结 12 740
死守一世寂寞
死守一世寂寞 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 03:55

    I prefer to use "filter" method and make a little easier work with counter:

    (function () {
        var divCounter = 0, divs;
    
        $(function () {
            divs = $('#div1, #div2, #div3');
            $('#toggle_value').click(function (e) {
                divs.hide() // hide other divs
                    .filter(function (index) { return index == divCounter % 3; }) // select appropriate div
                    .show('fast'); // and show it
    
                divCounter++;
            });
        });
    
    })();
    

提交回复
热议问题