根据点击的链接显示div?

时间:2014-05-31 21:30:43

标签: jquery html

这是一种非常常见的做法,你点击一个链接来显示其他一些div,根据点击的链接,div会改变..但是我赢了什么工作。有更好的方法吗?

演示:http://jsfiddle.net/wn2FE/2/

这是我的代码:

<nav>
    <a href="#">Info Div #1</a> 
    <a href="#">Info Div #2</a> 
    <a href="#">Info Div #3</a> 
    <a href="#">Info Div #4</a> 
</nav>

<div id="one">Lorem ipsum...</div>
<div id="two">Lorem ipsum...</div>
<div id="three">Lorem ipsum...</div>
<div id="four">Lorem ipsum...</div>

的jQuery

$(function(){
$("nav a").click(function() {
    switch($(this)) {
        case $(this).index() === 0:
            $("div#one").addClass("display").siblings().removeClass("display");

        case $(this).index() === 1:
            $("div#two").addClass("display").siblings().removeClass("display");

        case $(this).index() === 2:
            $("div#three").addClass("display").siblings().removeClass("display");

        case $(this).index() === 3:
            $("div#four").addClass("display").siblings().removeClass("display");
    }
});
});

我在控制台中看不到任何错误..但我的div不会出现。这也不是一个非常聪明的方法,也许有人可以提出更清洁的建议吗?

由于

6 个答案:

答案 0 :(得分:5)

如果没有开关/外壳并且使用ID,这将更容易 我推荐使用DIV的同一个课程,但是对于我刚刚使用$('div')的例子,因为那些是唯一的DIV&#39>

$(function () {
    $("nav a").click(function () {
        $('div').hide().eq($(this).index()).show();
    });
});

FIDDLE

答案 1 :(得分:2)

你的开关错了,你也没有休息。它应该是这样的:

$(function () {
    $("nav a").click(function () {
        switch ($(this).index()) {
            case 0:
                $("div#one").addClass("display").siblings().removeClass("display");
                break;

            case 1:
                $("div#two").addClass("display").siblings().removeClass("display");
                break;

            case 2:
                $("div#three").addClass("display").siblings().removeClass("display");
                break;
            case 3:
                $("div#four").addClass("display").siblings().removeClass("display");
                break;
        }
        return false;
    });


});

Demo

答案 2 :(得分:1)

您的switch语句的语法不正确:

$("nav a").click(function() {
    switch($(this).index()) {
        case 0:
            $("div#one").addClass("display").siblings().removeClass("display");
            break;
        case 1:
            $("div#two").addClass("display").siblings().removeClass("display");
            break;
        case 2:
            $("div#three").addClass("display").siblings().removeClass("display");
            break;
        case 3:
            $("div#four").addClass("display").siblings().removeClass("display");
            break;
    }
});

答案 3 :(得分:0)

使用.toggle()显示和隐藏div。

var $one = $("#one");

$("#link1").on("click", function(){
    $one.toggle();
});

以下是您的小提琴的更新 - fiddle

答案 4 :(得分:0)

通常做的是向要隐藏的div添加一个“隐藏”类,然后每当您单击一个链接时,就将该类删除到该div。使用jQuery执行此操作非常简单明了,这是一个有效的jsFiddle

这是HTML

    <nav>
    <a href="#" data-value="one">Info Div #1</a>    
    <a href="#" data-value="two">Info Div #2</a>    
    <a href="#" data-value="three">Info Div #3</a>  
    <a href="#" data-value="four">Info Div #4</a>   
</nav>

<div id="one" class="hide">It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).</div>
<div id="two" class="hide">There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which don't look even slightly believable. If you are going to use a passage of Lorem Ipsum, you need to be sure there isn't anything embarrassing hidden in the middle of text. All the Lorem Ipsum generators on the Internet tend to repeat predefined chunks as necessary, making this the first true generator on the Internet. It uses a dictionary of over 200 Latin words, combined with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is therefore always free from repetition, injected humour, or non-characteristic words etc.</div>
<div id="three" class="hide">The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham.</div>
<div id="four" class="hide">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</div>

jQuery的:

    $(function(){
$("nav a").click(function() {
    $('#'+$(this).attr('data-value')).removeClass('hide')
});
});

CSS:

    nav {
    border: 1em double lightgray;
    display: inline-block;
}

a {
    color: black;
    display: block;
} 

.hide {
    display: none;
}
div.display {
    display: block;
    width: 250px;
}

答案 5 :(得分:0)

您可以使用data自定义属性来&#34;告诉&#34; jQuery哪个div触发。请考虑以下事项:

HTML

<nav>
    <a href="#" data-id="one">Info Div #1</a> 
    <a href="#" data-id="two">Info Div #2</a> 
    <a href="#" data-id="three">Info Div #3</a> 
    <a href="#" data-id="four">Info Div #4</a> 
</nav>

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

的jQuery

$('a').on('click', function() {
    var div_id = $(this).data('id');

    $('.toggle_content').hide();
    $('#' + div_id).toggle();
});

工作示例:http://jsfiddle.net/44hvd/

注意,我在所有导航链接中添加了data-id。值等于各自的div&#39;标识。另外,我使用toggle_content类隐藏所有其他div,而不是使用$('div')作为选择器,这将是相当鲁莽的。