Show a div when click on related link and hide the others

我的未来我决定 提交于 2019-12-01 10:56:15

Use preventDefault()

        $("a").click(function (e) 
    {
            e.preventDefault();
        $("#" + $(this).attr("class")).show().siblings('div').hide();
    });

Demo Fiddle

You should specify # in the href:

<a href="#"></a>

Here is the demo: JSFiddle

You can add common class to all divs like

<div id="one" class="div">One</div>
<div id="two" class="div">Two</div>
<div id="three" class="div">Three</div>
<div id="four" class="div">Four</div>

js:

$("a").click(function (){         
    $(".div").hide();            
    $("#" + $(this).attr("class")).show().siblings('div').hide();
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!