简体   繁体   English

站点更改后仍保持打开状态的jQuery Dropdown –帮助新手

[英]jQuery Dropdown that stays open after site-change – help a newbie

I need a jQuery dropdown that stays open after a submenu-item was clicked and the user is forwarded to the subpage. 我需要一个jQuery下拉列表,该下拉列表在单击子菜单项并将用户转发到子页面后保持打开状态。

I'm toggling my dropdown with this code and I guess I need to add a class to the ul.submenu and toggle the visibility of this with CSS? 我正在用此代码切换下拉菜单,我想我需要在ul.submenu中添加一个类,并使用CSS切换可见性?

http://jsfiddle.net/erL8Lc0s/9/ http://jsfiddle.net/erL8Lc0s/9/

$(function () {

// Dropdown toggle
$('.dropdown-toggle a').click(function () {
    $(this).next('.sub-menu').toggle();

});

$(document).click(function (e) {
    var target = e.target;
    if (!$(target).is('.dropdown-toggle a') && !$(target).parents().is('.dropdown-toggle a')) {
        $('.sub-menu').hide();
    }
});

});

Unfortunately I'm a jQuery-noob and I can't get my head around this problem 不幸的是,我是一个jQuery菜鸟,我无法解决这个问题

There are a few ways to achieve this, however it's unlikely you'll get a straight-forward answer here, and here's why: 有几种方法可以实现此目的,但是您不太可能在这里得到直接的答案,这就是原因:

What you're trying to do is show an element, depending on what page the user is currently viewing. 您尝试做的是显示一个元素,具体取决于用户当前正在查看的页面。 There are a few ways to achieve this, but which one depends on the specific circumstances of the problem. 有几种方法可以实现此目的,但是哪种方法取决于问题的具体情况。 Here are a few ways: 以下是几种方法:

  1. You've stated you're using WordPress. 您已经说过您正在使用WordPress。 Great, WordPress makes the page ID visible, so within your theme you could easily add an override class to the element (such as class="show" ) depending on the page ID. 很好,WordPress使页面ID可见,因此您可以根据页面ID轻松地在主题内向元素添加替代类(例如class="show" )。 However, this means you'll have to keep a list of page IDs up-to-date. 但是,这意味着您必须保留最新的页面ID列表。

  2. Moving on from the above, you could use PHP to grab the current URI, and show the correct element based on that. 从上面继续,您可以使用PHP来获取当前URI,并基于该URI显示正确的元素。

  3. Just use WordPress's default menus. 只需使用WordPress的默认菜单即可。 You can amend the markup with a navigation crawler. 您可以使用导航搜寻器来修改标记。 It looks like you're using Bootstrap, so there's a premade one available . 看来您使用的是Bootstrap,所以有一个预制的 Now WordPress will automagically add active classes to the nav elements if you're on that page. 现在,如果您在该页面上,WordPress将自动将active类添加到nav元素。 You can then use CSS to keep those elements open. 然后,您可以使用CSS使这些元素保持打开状态。

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

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