使用jQuery显示/隐藏div然后显示/隐藏该div中的div

时间:2022-01-01 20:32:32

I have a series of links nested within a div. When one of those links is clicked, I'd like to hide the div with the link that was clicked and show another div. Then, if a link is clicked within that div, I'd like to change the div to yet another div.

我有一系列嵌套在div中的链接。当单击其中一个链接时,我想隐藏带有单击链接的div并显示另一个div。然后,如果在该div中单击了一个链接,我想将div更改为另一个div。

The html looks like this:

html看起来像这样:

<div id="main">
    <div class="item"><a href="">Link to div A</a></div>
    <div class="item"><a href="">Link to div B</a></div>
    <div class="item"><a href="">Link to div C</a></div>
</div>

<div id="a" style="display:none;">Link to div <a href="">B</a> and <a href="">C</a></div>
<div id="b" style="display:none;">Link to div <a href="">A</a> and <a href="">C</a></div> 
<div id="c" style="display:none;">Link to div <a href="">A</a> and <a href="">B</a></div> 

I'm getting a little tripped up with the jQuery on this one. Does anyone have any advice on how to get this working with jQuery? Showing/hiding a div seems straight forward, but doing that again within the divs is confusing me.

在这个问题上,我对jQuery进行了一些操作。有没有人对如何使用jQuery有任何建议?显示/隐藏div似乎很直接,但在div中再次这样做让我感到困惑。

Thanks!

1 个解决方案

#1


2  

Not sure if this is exactly what you're after, but I put together this jsFiddle for you to have a look at. See here.

不确定这是否正是你所追求的,但我把这个jsFiddle放在一起让你看看。看这里。

I added a few changes so you can identify which anchors relate to which div, so my HTML looks as follows:

我添加了一些更改,以便您可以确定哪个锚与哪个div相关,因此我的HTML如下所示:

<div id="main">
    <div class="item"><a href="#" name="a">Link to div A</a></div>
    <div class="item"><a href="#" name="b">Link to div B</a></div>
    <div class="item"><a href="#" name="c">Link to div C</a></div>
</div>

<div class="item" id="a" style="display:none;">
    Link to div <a href="#" name="b">B</a> and <a href="#" name="c">C</a>
</div>
<div class="item" id="b" style="display:none;">
    Link to div <a href="#" name="a">A</a> and <a href="#" name="c">C</a>
</div>
<div class="item" id="c" style="display:none;">
    Link to div <a href="#" name="a">A</a> and <a href="#" name="b">B</a>
</div> 

Then, just a simple use of jQuery and it seems to work as you've described. Have a look at the jsFiddle I made and let me know if that's what you're after.

然后,只是简单地使用jQuery,它似乎像你所描述的那样工作。看看我制作的jsFiddle,让我知道你是不是想要的。

$(document).ready(function() {

    // Hook up the first divs
    $(".item a").click(function() {

        // Get the target from the name of the anchor
        var targetDiv = $(this).attr("name");

        // Show the new div and hide the parent div
        $("#" + targetDiv).css("display", "");
        $(this).parents("div:last").css("display", "none");

    });

});

#1


2  

Not sure if this is exactly what you're after, but I put together this jsFiddle for you to have a look at. See here.

不确定这是否正是你所追求的,但我把这个jsFiddle放在一起让你看看。看这里。

I added a few changes so you can identify which anchors relate to which div, so my HTML looks as follows:

我添加了一些更改,以便您可以确定哪个锚与哪个div相关,因此我的HTML如下所示:

<div id="main">
    <div class="item"><a href="#" name="a">Link to div A</a></div>
    <div class="item"><a href="#" name="b">Link to div B</a></div>
    <div class="item"><a href="#" name="c">Link to div C</a></div>
</div>

<div class="item" id="a" style="display:none;">
    Link to div <a href="#" name="b">B</a> and <a href="#" name="c">C</a>
</div>
<div class="item" id="b" style="display:none;">
    Link to div <a href="#" name="a">A</a> and <a href="#" name="c">C</a>
</div>
<div class="item" id="c" style="display:none;">
    Link to div <a href="#" name="a">A</a> and <a href="#" name="b">B</a>
</div> 

Then, just a simple use of jQuery and it seems to work as you've described. Have a look at the jsFiddle I made and let me know if that's what you're after.

然后,只是简单地使用jQuery,它似乎像你所描述的那样工作。看看我制作的jsFiddle,让我知道你是不是想要的。

$(document).ready(function() {

    // Hook up the first divs
    $(".item a").click(function() {

        // Get the target from the name of the anchor
        var targetDiv = $(this).attr("name");

        // Show the new div and hide the parent div
        $("#" + targetDiv).css("display", "");
        $(this).parents("div:last").css("display", "none");

    });

});