简体   繁体   English

在jQuery UI手风琴菜单中激活当前内容部件

[英]Activate Current Content Part in jQuery UI Accordion Menu

I'm trying to figure out how to dynamically activate the correct content part of a jQuery UI Accordion menu depending on the page currently being viewed. 我试图弄清楚如何根据当前正在查看的页面动态激活jQuery UI Accordion菜单的正确内容部分。 I've searched extensively and it seems like others have had issues with this in the past, but I haven't yet found a solution that works for me. 我已经进行了广泛的搜索,似乎过去其他人对此有疑问,但是我还没有找到适合我的解决方案。 I know that active can be set to open a certain index of the menu, but I need to do this dynamically. 我知道可以将active设置为打开菜单的某个索引,但是我需要动态地执行此操作。

I'm thinking that I can achieve what I want using the activate method, I just can't seem to figure it out. 我在想我可以使用activate方法实现我想要的,只是似乎无法弄清楚。 I'd like to stay away from setting cookies as that usually won't work with back/forward buttons and direct navigation via a specific url. 我想避免设置Cookie,因为通常无法通过后退/前进按钮和通过特定URL的直接导航来工作。 Anyone have any ideas? 有人有想法么? Thanks in advance! 提前致谢!

Here is the simplified structure of my menu: 这是菜单的简化结构:

<div id="menu">
    <div id="sections">
        <div class="grouping accordion">
            <a id="heading1" href="#">Heading #1</a>
            <div class="sub-items">
                <a href="/item1">Item #1</a>
                <br />
                <a href="/item2">Item #2</a>
            </div>
        </div>
        <div class="grouping accordion">
            <a id="heading2" href="#">Heading #2</a>
            <div class="sub-items">
                <a href="/item4">Item #4</a>
                <br />
                <a href="/item5">Item #6</a>
            </div>
        </div>
    </div>
</div>

And here is my jQuery Accordion init: 这是我的jQuery手风琴初始化:

$('#sections').accordion({
    header: '> .accordion > a',
    autoHeight: false,
    collapsible: true,
    active: false,
    animated: 'slide'
});

So if you are currently on the /item4 page for example, the group under Heading #2 should be expanded. 因此,例如,如果您当前在/item4页面上,则标题#2下的组应被展开。

EDIT: I found what seems to be a pretty good solution and posted that as an answer below, hopefully this will help someone with a similar problem! 编辑:我发现什么似乎是一个很好的解决方案,并将其发布为下面的答案,希望这将对遇到类似问题的人有所帮助!

To activate a specific tab, you'll want to use the accordion('activate', index) method. 要激活特定的选项卡,您将要使用accordion('activate', index)方法。 Example: 例:

$( "#sections" ).accordion('activate', 2);

However, you will need something that defines an index key per page. 但是,您将需要一些定义每页索引键的内容。 You can probably even generate this dynamically. 您甚至可以动态生成它。 I would probably create a Pages object: 我可能会创建一个Pages对象:

Pages = {
    "heading1" : {
        "id": 1,
        "is_active": false,
        "items": {
            "item1": {
                "href": "item1",
                "name": "Item #1"
            },
            "item2": {
                "href": "item2",
                "name": "Item #2"
            }
        }
    },

    "heading2" : {
        /* etc*/    
    },

}

With some hackish jQuery magic, you can loop through your headings 有了一些骇人听闻的jQuery魔术,您可以遍历标题

var active_heading_index = null;

$.each(Pages, function(heading) {
    $.each(heading.items, function(item) {
        if($(location).attr('href') == item.href) {
            heading.is_active = true;
            // or
            active_heading_index = heading.id
        }
    });
});

if(active_heading_index) $( "#sections" ).accordion('activate', active_heading_index);

Anyhow, I'm sure there are cleaner and more efficient ways of doing it. 无论如何,我敢肯定有更清洁,更有效的方法。

While working on some CSS for the active headings on the menu I stumbled on a pretty clean and easy solution. 在为菜单上的活动标题使用一些CSS时,我偶然发现了一个非常干净,简单的解决方案。 Looks like I might have been overthinking things! 看来我可能已经想得太多了!

Using the same HTML as in the question, here's the JavaScript that is working for me: 使用与问题中相同的HTML,以下是对我有用的JavaScript:

//accordion menu
$('#sections').accordion({
    header: '> .accordion > a',
    autoHeight: false,
    collapsible: true,
    active: '.selected',
    animated: 'slide'
});

//add currentPage class to current menu item based on url
var url = window.location.href;
url = url.substr(url.lastIndexOf("/") + 1);
$("#sections").find("a[href='" + url + "']").addClass("currentPage");

//get id of header anchor tag
var headerId = $(".currentPage").parents(".accordion").children("a").attr("id");

//check if headerId is set, if so activate that id
if (headerId) {
    $('#sections').accordion('option', 'animated', false);
    $('#sections').accordion('activate', $('#' + headerId));
    $('#sections').accordion('option', 'animated', 'slide');
}

This solution is pretty simple, it gets the current page from the url and compares it against each link in the accordion menu. 该解决方案非常简单,它从url获取当前页面并将其与手风琴菜单中的每个链接进行比较。 If it finds a match, it gives that link a class of currentPage (which allows us to then style that link accordingly via css). 如果找到匹配项,它将为该链接提供currentPage类(这使我们可以随后通过CSS对链接进行相应样式设置)。 Then it looks for a parent of that link with a class of .accordion , finds the first child link (the accordion header) and grabs the header's id. 然后,它使用.accordion类查找该链接的父级,找到第一个子链接(手风琴标题)并获取标题的ID。 Assuming a header id has been found, we can then use the activate method to expand the correct section. 假设已找到标头ID,则可以使用activate方法扩展正确的部分。

If you are going back to the server for every page click (standard non Ajaxy way), the server can add a "selected" class to the proper node. 如果每次单击页面都返回服务器(标准的非Ajaxy方式),则服务器可以将“选定的”类添加到适当的节点。 So you'd get back something like this at the client (I'm only writing the essential code, skipping most of the labels). 因此,您将在客户端获得类似的信息(我只是编写基本代码,跳过了大部分标签)。

<ul id="menu">
  <li>
    <ul>
      <li>
      </li>
      <li class="selected">
        <a href="">Menu 102</a>
      </li>
    <ul>
  </li>
  <li>
  ...
  </li>
<ul>

Then simply find the proper index to give to the activate property of the accordion. 然后只需找到适当的索引即可赋予手风琴的activate属性。

$(document).ready(function() {
    var index = $("li.selected").parents("li").last().index();
    $("#menu").accordion({
      active: index,
      collapsible: true
    });
});

The parents("li").last() jQuery returns the top most element. jQuery的Parents parents("li").last()返回最上面的元素。 This only works if you only have one sub element with the class "selected", which should be the case for your menu. 仅当您只有一个带有“ selected”类的子元素时才有效,这对于菜单来说就是这种情况。

I did it using this code: 我是使用以下代码完成的:

var newsNum = parseInt(window.location.hash.slice(1));
$(document).ready(function(){
    $("#menu").accordion('activate', newsNum );
});

And the url looks like example.com/index.html#1 网址看起来像example.com/index.html#1

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

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