Onclick Show / Hide Multiple Divs Jquery

前端 未结 5 1267
萌比男神i
萌比男神i 2021-01-22 14:24

I need to create navigation that shows / hides multiple divs.

Here is what I need to be able to do: If a user clicks on any of the \"options

5条回答
  •  独厮守ぢ
    2021-01-22 14:37

    You should use data attributes, as just target is'nt really valid. I changed it to data-target and did:

    $('.showSingle').on('click', function () {
        $(this).addClass('selected').siblings().removeClass('selected');
        $('.targetDiv').hide();
        $('#div' + $(this).data('target')).show();
    });​
    

    FIDDLE

    on() will only work in jQuery 1.7+, so if using an older version of jQuery (your fiddle is), stick with click().

提交回复
热议问题