A
A
Art222016-06-19 14:24:57
User navigation
Art22, 2016-06-19 14:24:57

How to fix the dropdown menu so that it doesn't close when the page is opened?

How to fix the dropdown menu so that it doesn't close when the page is opened? here is a link to the site super-gun.ru
Example:
Click on
Pneumatic Pistols opens a drop-down menu
, click on GAMO Pistols opens the directory is highlighted GAMO Pistols and the menu does not close.
Menu code

function initMenu() {
  jQuery(".vmenu li.parent > a").click(function(eventObject){return false});
  jQuery('.vmenu ul').hide();
  jQuery('.vmenu li.active ul').show();
  jQuery('.vmenu li a').click(
    function() {
        jQuery(this).next().slideToggle('normal');	
      }
    );
  }
jQuery(document).ready(function() {initMenu();});

code to close
function allClose(){
  var list = document.getElementById("menu").getElementsByTagName("ul");
  for(var i=0;i<list.length;i++){
    list[i].style.display = "none";
  }
}
function openMenu(node){
  var subMenu = node.parentNode.getElementsByTagName("ul")[0];
  subMenu.style.display == "none" ? subMenu.style.display = "block" : subMenu.style.display = "none";
}

onload="allClose()"> is inserted to close the menu.
the link is set to the code onclick="openMenu(this);return false"

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
semki096, 2016-06-19
@semki096

As an option, place on each of the pages a script that adds the display: block property to the item.

E
enkos, 2016-06-19
@enkos

Try replacing your code with this one. Should work. This saves one open menu and does not highlight the active menu item.

var menu = jQuery('#menu');

menu.find('ul').each(function(index, value) {
  value.id = 'opened_section_' + index;
});


function initMenu() {
  var openedMenuElem = JSON.parse(localStorage.getItem('superGunSettings')).openedMenuId;
  jQuery(".vmenu li.parent > a").click(function(eventObject){return false});
  jQuery('.vmenu ul').hide();
  jQuery('.vmenu li.active ul').show();
  jQuery('.vmenu li a').click(
    function() {
        jQuery(this).next().slideToggle('normal');	
      }
    );
  jQuery('#' + openedMenuElem).show();
  
  }
jQuery(document).ready(function() {initMenu();});


function allClose(){
  var list = document.getElementById("menu").getElementsByTagName("ul");
  for(var i=0;i<list.length;i++){
    list[i].style.display = "none";
  }
}
function openMenu(node){
  var subMenu = node.parentNode.getElementsByTagName("ul")[0];
  localStorage.setItem('superGunSettings', JSON.stringify({openedMenuId: subMenu.id}));
  subMenu.style.display == "none" ? subMenu.style.display = "block" : subMenu.style.display = "none";
}

A
Art22, 2016-06-20
@Art22

<ul id="menu">
<li><a href="111" onclick="openMenu(this);return false"><img src="/images/menu/1.jpg"/>111</a>
<ul>
<li><a href="1">1</a></li>
<li><a href="2">2</a></li>
</ul>
</ul>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question