简体   繁体   English

jQuery切换允许父元素的默认href和父元素应显示子元素(切换)

[英]jQuery toggle allow default href of parent element AND parent element should show child elements (toggle)

I would like to open a link for example "1" (1.html) and afterwards it should show (toggle) all child elements of the site 1 (1-1, 1-2,...). 我想打开一个链接,例如“ 1”(1.html),然后它应该显示(切换)站点1(1-1、1-2,...)的所有子元素。 I did a research in google and of course also in stackoverflow. 我在Google进行了研究,当然也在stackoverflow中进行了研究。

I only found a similar issue of someone who uses wordpress, which it comes nearly to my wish, but I am not a expert like you guys: http://premium.wpmudev.org/forums/topic/jquery-toggle-and-allow-default 我只发现了一个使用wordpress的人的类似问题,这几乎符合我的愿望,但我不是像你们这样的专家: http : //premium.wpmudev.org/forums/topic/jquery-toggle-and-允许违约

<div class="col col-md-3">
<nav id="#mobile-nav" class="nav clearfix" role="navigation">
    <ul class="nav" id="menu-flag-menu">
        <li><a href="index.html">Startseite</a></li>
        <li><a href="1.html">1</a>
            <ul>
                <li><a href="1-1.html">1-1</a></li>
                <li><a href="1-2.html">1-2</a></li>
                <li><a href="1-3.html">1-3</a></li>         
            </ul>
        </li>   
        <li><a href="2.html">2</a>
            <ul>
                <li><a href="2-1.html">2-1</a></li>
                <li><a href="2-2.html">2-2</a></li>
                <li><a href="2-3.html">2-3</a></li>
                <li><a href="2-4.html">2-4</a></li>
            </ul>
        </li>           
        <li><a href="#3">3</a>
            <ul>
                <li><a href="#3.1">3.1</a></li>
                <li><a href="#3.2">3.2</a></li>
            </ul>
        </li>
    </ul>
</nav>

My jQuery toggle function: 我的jQuery切换功能:

$("#menu-flag-menu li a").click(function(){ 
// close all opened sub menus
$("#menu-flag-menu > li > ul > li > ul  > li > ul").slideUp();
// http://stackoverflow.com/questions/2534162/simple-jquery-toggle-of-children
var ul = $(this).next("ul");
if (ul.is(":hidden")) {
    ul.slideDown();
} 
else {
    ul.slideUp();
} });

My CSS: 我的CSS:

ul#menu-flag-menu > li > ul { display:none; }

I hope you can help me, thanks! 希望您能帮助我,谢谢! This is my first question, I don't double post and I try to used my brain, google and stackoverflow, before I ask. 这是我的第一个问题,我不重复发表文章,在我问之前,我尝试使用我的大脑,google和stackoverflow。 :) :)

I guess the JSfiddle would not help for imagine my demand, but here it is: http://jsfiddle.net/tFh9w/1/ 我猜想JSfiddle不能满足我的需求,但是这里是: http : //jsfiddle.net/tFh9w/1/

http://jsfiddle.net/tFh9w/8/ http://jsfiddle.net/tFh9w/8/

Because you are using static HTML files, unless you want to make the menu different in each file (I hope you're at least including the menu using SSI), you need JS to detect what page the browser has opened, and to then trigger the opening of the relevant menu item. 因为您使用的是静态HTML文件,否则除非您希望每个文件中的菜单都不同(我希望您至少包括使用SSI的菜单),否则您需要使用JS来检测浏览器已打开的页面,然后触发相关菜单项的打开。

First of all let's make it easy and add a level1 class to the level 1 menu elements. 首先,让我们变得容易,并向level 1菜单元素添加一个level1类。

<li class="level1"><a href="1.html">1</a>
    <ul>
        <li><a href="1-1.html">1-1</a></li>
        <li><a href="1-2.html">1-2</a></li>
        <li><a href="1-3.html">1-3</a></li>         
    </ul>
</li>   
<li class="level1"><a href="2.html">2</a>
      etc etc

Then we find the last part of the URL, and use jquery to find the anchor who's href matches it. 然后,我们找到URL的最后一部分,并使用jquery查找与href匹配的锚。 Then we check if it's a parent or child (only works for two levels, but can be adapted for more) and we open the correct menu item. 然后,我们检查它是父级还是子级(仅适用于两个级别,但可以适应更多级别),然后打开正确的菜单项。

$(function(){
    var hrefs = $.trim(window.location.href).replace(/\/$/, "").split('/');
    var href = hrefs[hrefs.length -1];
    href = '1.html'; // we have to cheat in jsfiddle. remove this line in production
    // href = '1-1.html';
    $('#menu-flag-menu a[href="' + href + '"]').each(function(){
        // what are we opening - the child of this link or the parent?

        if ($(this).parent().hasClass('level1')) $ul = $(this).parent().children('ul');
        else $ul = $(this).closest('ul');

        $ul.slideDown();
    });

});

In the example I've had to cheat because in jsfiddle the href is always going to be _display 在这个例子中,我不得不在的jsfiddle在href因为欺骗总是会被_display

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

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