开发者

Modify Existing Questions answer

开发者 https://www.devze.com 2023-02-12 20:47 出处:网络
Just a quick questions. Im new to this site but this link Show/Hide开发者_开发技巧 multiple DIVs with Select using jQuery

Just a quick questions. Im new to this site but this link Show/Hide开发者_开发技巧 multiple DIVs with Select using jQuery

I just wanted to know how to change the code so its not in a drop down select but an actual link.

If anyone could help me that would be badass!!

I have looked everywhere and this is closest to what i could find.

Thanks everyone. Woot!


I modified the accepted answer from this question how to show/hide divs by select.(jquery) the question the OP linked just so he could see the similarities between doing the two. As it was pointed out this naming convention is not good at all.

Markup

<a id="1" href="#">Link one</a>
<a id="2" href="#">Link two</a>
<a id="3" href="#">Link three</a>


<div id="div1">content here 1</div>
<div id="div2">content here 2</div>
<div id="div3">content here 3</div>    

JS

$(function(){
    $('#div1, #div2, #div3').hide();

    $('a').click(function() {
       $('#div1, #div2, #div3').hide();
       $('#div' + $(this).attr('id')).show();
    });
}

Demo http://jsfiddle.net/kJGcE/

And here is a better way

Markup

<a class="divHide" href="#">Link one</a>
<a class="divHide" href="#">Link two</a>
<a class="divHide" href="#">Link three</a>


<div class="divShow">content here 1</div>
<div class="divShow">content here 2</div>
<div class="divShow">content here 3</div>

JS

$('a.divHide').click(function() {
   $('.divShow').hide();
   $('.divShow').eq($(this).index()).show();
});

Demo http://jsfiddle.net/kJGcE/2/

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号