0

I'm using this jQuery accordion menu found here jQuery Vertical Accordion Menu Plugin. The plugin is working well without much customization. The feature that I was really looking for that this plugin offers is persistent menu state after a page refresh. This plugin uses cookies to accomplish this. On to the problem...

If you click outside the accordion menu the state is still maintained. I'm trying to figure out a way to collapse the entire menu when a link is clicked outside of the accordion menu.

My first inclination was to find some way to delete or reset the data in state saving cookie when the window.location was equal to a certain URL. (At the moment it I be happy if when returning to the home page that the menu was completely collapsed).

I'm thinking the best solution would be some jQuery that if an was clicked outside of #Accordion-Navigation was clicked that it would reset the cookie or collapse the menu.

I'm open to anything.

I tried the following with no luck.

jQuery(document).ready(function(jQuery){
    var siteurl = "http://myhomepageurl.com/index.php";
if (window.location.href == siteurl) {
    jQuery.cookie('dcjq-accordion-1', null, { path: '/'});
    }
});

Any suggestions at a more elegant solution would be greatly appreciated! Thanks for taking the time to have a look in advance!

4

2 回答 2

0

有一个技巧可以做到这一点,只需在你的 html 和 body 元素上绑定一个事件处理程序。如果点击了手风琴,则停止传播,否则折叠手风琴。

$(function () {
  $('html').on('click', collapseAccordion);
  $('body')
    .on('click','.accordion', function (e) { e.stopPropagation() });
});

您应该创建一个collapseAccordion通过 api 调用折叠手风琴的函数。

于 2012-07-24T08:43:08.157 回答
0
$(function () {
  $('body').mousedown(function (e) { 
    if($(e.target).closest('.accordion').length != 1){
       //click was outside accordion,
       // so close it
    }      
});
于 2012-07-24T10:11:05.193 回答