简体   繁体   English

如何使用jquery显示/隐藏特定的多层嵌套div?

[英]How to show/hide specific multi-level nested divs with jquery?

i'm stuck with this problem: I want to show specific div's depending on the choice of the user. 我遇到了这个问题:我想根据用户的选择显示特定的div。 The div's are nested and best should be targeted by some id. div是嵌套的,最好使用一些id作为目标。

This is the algorithm/sitemap i want to visualize on the screen: 这是我想在屏幕上可视化的算法/站点地图:

导航/站点地图

And this is my first draft: 这是我的初稿:

HTML: HTML:

<div class="buttons">
    <button class="showButton" target="1">Button 1</button>
    <button class="showButton" target="6">Button 2</button>
    <button class="showButton" target="3">Button 3</button>
</div>
<div id="div1" class="targetDiv">Content of div 1.<br />
    <button class="showButton" target="4">open 4</button>
    <button class="showButton" target="5">open 5</button>
    <div id="div4" class="targetDiv">This is div 4.</div>
    <div id="div5" class="targetDiv">This is div 5.</div>
</div>
<div id="div2" class="targetDiv">Content of div 2.<br/>
    <button class="showButton" target="9">open 9</button>
</div>
<div id="div3" class="targetDiv">Content of div 3.<br/>
    <button class="showButton" target="6">open 6</button>
    <div id="div6" class="targetDiv">Content of div 6.<br />
        <button class="showButton" target="9">open 9</button>
        <button class="showButton" target="10">open 10</button>
    </div>
</div>

JavaScript: JavaScript:

jQuery(function () {
    jQuery('.showButton').click(function () {
        jQuery('.targetDiv').hide();
        jQuery('#div' + $(this).attr('target')).show('slide');
    });
});

http://jsfiddle.net/5TDg9/6E2Gv/ http://jsfiddle.net/5TDg9/6E2Gv/

If you click on button in div 2, the div 9 should show up. 如果单击div 2中的按钮,则应该显示div 9。

Any ideas? 有任何想法吗?


added on 30.03.14: 添加于30.03.14:

For more detailed explanation, i want to show you an example of usage for the script: 有关更详细的说明,我想向您展示该脚本的用法示例:

Imagine this algorithm for example : 想象一下这个算法,例如:

资料来源:骨髓移植(2003)32,459–469。 doi:10.1038 / sj.bmt.1704163 [Image from: Bone Marrow Transplantation (2003) 32, 459–469. [图片来源:骨髓移植(2003)32,459-469。 doi:10.1038/sj.bmt.1704163] doi:10.1038 / sj.bmt.1704163]

If you look at the last "items" of this algorithm three, there are several ways to get there. 如果您看一下算法3的最后一个“项目”,那么有几种方法可以到达那里。 On the right side for example, there is one last "item" called "Dose escalate/consider trials for resistant disease" and there are two ways to get to this item (one is over "inadequate response" the other is over "No cytogenic response"). 例如,在右侧,有一个最后的“项目”,称为“针对耐药性疾病的剂量递增/考虑试验”,并且有两种方法可以达到该目的(一种是“反应不足”,另一种是“无细胞原性”)。响应”)。

With the script I'm searching for here, I want the user to be asked wich way to go and to visualize his choosen path on the screen. 借助我在这里搜索的脚本,我希望以一种通向用户的方式询问用户,并在屏幕上可视化他选择的路径。

I made my first attempts with a simple tabs html structure and just duplicating the last "items". 我用一个简单的html标签结构进行了第一次尝试,只是复制了最后一个“项目”。 This works fine but is quite redundant. 这可以正常工作,但是非常多余。 Hope someone comes up with a more suitable solution. 希望有人提出一个更合适的解决方案。

Method 4 : This method takes all the divs out of the nested structure on the HTML side, and uses jQuery to re-organize where they appear on the page. 方法4 :此方法将所有div从HTML一侧的嵌套结构中移出,并使用jQuery重新组织它们在页面上的显示位置。

HTML (I've used some simple comments to help define the branches, since they are no longer obvious by simply viewing the page code (your diagram will also be helpful here)): HTML(我使用了一些简单的注释来帮助定义分支,因为通过简单地查看页面代码,它们就不再显而易见了(您的图表在这里也将有所帮助)):

<h1>Example of nested divs toggle</h1>

<!-- Top Level Menu -->
<div id="div0" class="buttons">
    <button class="showButton" target="1">Button 1</button>
    <button class="showButton" target="2">Button 2</button>
    <button class="showButton" target="3">Button 3</button>
</div>

<!-- div1 Branch -->
<div id="div1" class="targetDiv">Content of div 1.<br />
    <button class="showButton" target="4">open 4</button>
    <button class="showButton" target="5">open 5</button>
</div>
<div id="div4" class="targetDiv">Content div 4.</div>
<div id="div5" class="targetDiv">This is div 5.</div>

<!-- div2 Branch -->
<div id="div2" class="targetDiv">Content of div 2.<br/>
    <button class="showButton" target="9">open 9</button>
</div>

<!-- div3 Branch -->
<div id="div3" class="targetDiv">Content of div 3.<br/>
    <button class="showButton" target="6">open 6</button>
</div>
<div id="div6" class="targetDiv">Content of div 6.<br />
    <button class="showButton" target="9">open 9</button>
    <button class="showButton" target="10">open 10</button>
</div>
<div id="div9" class="targetDiv">Content of div 9.<br/></div>
<div id="div10" class="targetDiv">Content of div 10.<br/></div>

Javascript (In this set, I use an array to keep track of which menu items you are currently showing. Then reverse-traverse that array to remove menu items which are no longer valid (in the case of changing the path at a higher step). Then I use insertAfter to re-organize the position of the HTML element within the menu structure): Javascript(在此示例中,我使用一个数组来跟踪当前显示的菜单项。然后反向遍历该数组以删除不再有效的菜单项(在更高的步骤更改路径的情况下)然后,我使用insertAfter来重新组织HTML元素在菜单结构内的位置):

//declare array with divs to show
var menuArray = ['0'];

$(function () {
    $('.showButton').click(function () {
        //get target div to show
        var targetID = $(this).attr('target');

        //hide all divs
        $('.targetDiv').hide();

        //show all menu items in chain
        var found = false;

        //get containing div target number
        var current = $(this).parent().attr('id').toString().charAt(3);
        var length = menuArray.length;

        //remove elements of array lower than clicked button
        for(var i=0; i<length; i++) {
            //check if next menu item is a button just pressed and never delete root menu (elem == 0)
            if(menuArray[0] == current || menuArray[0] == 0) {
                found = true;
            }
            //hide div if no longer in menu structure and remove from menuArray[0]
            if(!found) {
                $('#div' + menuArray[0]).hide();
                menuArray.shift();
            }
        };

        //add new target to beginning of array
        menuArray.unshift(targetID);

        //show each element in the menu structure
        menuArray.forEach(function(elem, index) {
            if(index != 0) {
                $('#div' + elem).show();
            }
        });

        //get div of target to show sliding effect
        var targetDiv = $('#div' + menuArray[0]);

        //show the div as the last element on the page
        targetDiv.insertAfter('#div' + menuArray[1]);

        //slide out last div
        targetDiv.show('slide');
    });
});

Here is the fiddle: http://jsfiddle.net/6E2Gv/66/ 这是小提琴: http : //jsfiddle.net/6E2Gv/66/

Method 3 : This method uses the parent() to show divs up the menu structure. 方法3 :此方法使用parent()来显示菜单结构的div。 I also returned to your original naming structure, in case you still wanted to use it. 如果您仍然想使用它,我还返回了原始的命名结构。

HTML: HTML:

<h1>Example of nested divs toggle</h1>

<div class="buttons">
    <button class="showButton" target="1">Button 1</button>
    <button class="showButton" target="2">Button 2</button>
    <button class="showButton" target="3">Button 3</button>
</div>
<div id="div1" class="targetDiv">Content of div 1.<br />
    <button class="showButton" target="4">open 4</button>
    <button class="showButton" target="5">open 5</button>
    <div id="div4" class="targetDiv">Content div 4.<br />
        <div id="div10" class="targetDiv">Content of div 10.<br/></div>
    </div>
    <div id="div5" class="targetDiv">This is div 5.</div>
</div>
<div id="div2" class="targetDiv">Content of div 2.<br/>
    <button class="showButton" target="9">open 9</button>
</div>
<div id="div3" class="targetDiv">Content of div 3.<br/>
    <button class="showButton" target="6">open 6</button>
    <div id="div6" class="targetDiv">Content of div 6.<br />
        <button class="showButton" target="9">open 9</button>
        <button class="showButton" target="10">open 10</button>
        <div id="div9" class="targetDiv">Content of div 9.<br/>
        </div>
    </div>
</div>

Javascript: Javascript:

$(function () {
    $('.showButton').click(function () {
        //hide all divs
        $('.targetDiv').hide();
        //get div of target number
        var targetDiv = $('#div' + $(this).attr('target'));

        var targetDivParent = targetDiv;
        while(targetDivParent.parent().attr('id')) {
            //show parent div
            targetDivParent.parent().show();
            //get next parent
            targetDivParent = targetDivParent.parent();
        }

        //slide out last div
        targetDiv.show('slide');
    });
});

Here is the fiddle: http://jsfiddle.net/6E2Gv/54/ 这是小提琴: http : //jsfiddle.net/6E2Gv/54/

Method 2 : OK, so here's a version where the javascript will not need changed, but requires you to label your branches appropriately. 方法2 :确定,所以这里是不需要更改javascript的版本,但是需要您适当标记分支。 (Ex: if div 04 is nested inside 01, it needs to be named '01-04' to show its path). (例如:如果div 04嵌套在01内部,则需要将其命名为“ 01-04”以显示其路径)。

Here is the fiddle: http://jsfiddle.net/6E2Gv/41/ 这是小提琴: http : //jsfiddle.net/6E2Gv/41/

Method 1 : Here's a version that works with your menu structure. 方法1 :这是与您的菜单结构一起使用的版本。 It's a little more tedious, but I used the switch statement to show the appropriate nested menus. 这有点乏味,但是我使用switch语句显示了适当的嵌套菜单。

http://jsfiddle.net/6E2Gv/37/ http://jsfiddle.net/6E2Gv/37/

Original Post : Do you need the entire menu structure to show up? 原帖 :您需要显示整个菜单结构吗? If so, your usage of nested divs won't work while hiding all ".targetDiv". 如果是这样,则在隐藏所有“ .targetDiv”时无法使用嵌套的div。

However, if you don't need the entire menu structure, you could take all the divs out of the nests and only show the top level and selected one, as such: 但是,如果您不需要整个菜单结构,则可以将所有div从嵌套中移出,仅显示顶层并选择其中一个,如下所示:

http://jsfiddle.net/6E2Gv/30/ http://jsfiddle.net/6E2Gv/30/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM