简体   繁体   English

JS下拉菜单中的链接不会重定向

[英]Link in JS dropdown menu does not redirect

So I have had at this all day, but not being a good JS coder it lead me here. 因此,我整天都在这里工作,但是由于不是一个好的JS编码器,这使我陷入了困境。

When I click on the link in the dropdown menu leading to "test.html" nothing happens. 当我单击下拉菜单中指向“ test.html”的链接时,没有任何反应。 It redirects nowhere, even if the browser clearly shows that it is the "test.html" link that is active on hovering. 即使浏览器清楚地表明悬停时处于活动状态的“ test.html”链接也不会重定向。

Here is the JS code. 这是JS代码。

var timeout    = 500;
var closetimer = 0;
var ddmenuitem = 0;

function headermenu_open(event)
{
   headermenu_canceltimer();
   headermenu_close();
   var submenu = $(this).find('ul');
    if(submenu){
        ddmenuitem = submenu.css('visibility', 'visible');
        return false;
    }
    return true;
}

function headermenu_close()
{  if(ddmenuitem) ddmenuitem.css('visibility', 'hidden');}

function headermenu_timer()
{  closetimer = window.setTimeout(headermenu_close, timeout);}

function headermenu_canceltimer()
{  if(closetimer)
   {  window.clearTimeout(closetimer);
      closetimer = null;}}

$(document).ready(function()
{  $('#headermenu li').bind('click', headermenu_open);
   $('#headermenu > li').bind('mouseout',  headermenu_timer);
   $('#headermenu > li').bind('mouseover', headermenu_canceltimer);
});


document.onclick = headermenu_close;

And this is a part of the menu. 这是菜单的一部分。

<ul id="headermenu">
    <li><a href="#">Høyttalere</a>
        <ul>
            <li><a href="pages/test.html">Test</a></li>
        </ul>
    </li>

If anyone has the right snippet to add or can see a mistake in the code I will be extremely happy! 如果任何人都有正确的代码段添加或在代码中看到错误,我将非常高兴!

It is because of this line of code 因为这行代码

if(submenu){
   ddmenuitem = submenu.css('visibility', 'visible');
   return false;
}

It goes into this block and return false 它进入此块并返回false

Which is equivalent of calling e.preventDefault() and e.stopPropagation() because of which the link does not follow. 这等效于调用e.preventDefault()e.stopPropagation() ,因此链接不会跟随。

Also

if(subMenu) // returns truth y value.. As it is a empty jQuery Object if(subMenu) //返回if(subMenu) y值。因为它是一个空jQuery对象

A better check would be is to check the length 更好的检查方法是检查长度

if(subMenu.length)

Code

function headermenu_open(event){

   event.stopPropagation(); // need to stop the Propagation first
   headermenu_canceltimer();
   headermenu_close();
   var submenu = $(this).find('ul');
    if(submenu.length){
        ddmenuitem = submenu.css('visibility', 'visible');
        return false;
    }
    return true;
}

Working Fiddle 工作小提琴

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

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