简体   繁体   English

为什么我的锚标签不起作用?

[英]Why do my anchor tags not work?

I have made a drop down menu using un-ordered lists, list items, jQuery and CSS. 我使用无序列表,列表项,jQuery和CSS制作了一个下拉菜单。 None of the menu items (which are anchors) work when I click on them. 当我单击菜单项(即锚点)时,它们均无作用。 All that happens when I click the anchor is the menu disappears. 当我单击锚点时,所有发生的事情就是菜单消失了。 If I right click an anchor and then open it in a new tab, it does redirect as I expect. 如果我右键单击一个锚点,然后在新选项卡中将其打开,则它确实会按预期进行重定向。 Here is a jFiddle that shows my code and the issue at hand. 是一个显示我的代码和当前问题的jFiddle。 Note that the menu is somewhat hard to open, you have to click the bottom portion of the icon. 请注意,菜单有些难以打开,您必须单击图标的底部。 If anyone could advise as to why the anchors do not open properly it would be greatly appreciated. 如果有人能建议为什么锚不能正确打开,将不胜感激。

HTML: HTML:

<span class="menu_item" id="settings">
    <ul>
        <li>
            <a href="#" onclick="return false;">&#9788;</a>
            <ul>
                <li>
                    <a href="/admin.php">Admin Center</a>
                </li>
                <li>
                    <a href="/login/process.php">Logout</a>
                </li>
            </ul>
        </li>
    </ul>
</span>

The return false; return false; you have in settings_open does two things, one of which is cancelling the default action (following the link). 您在settings_open中有两件事,其中之一是取消默认操作(遵循链接)。 Remember that clicks bubble from descendant elements up to ancestor elements. 请记住,单击会从后代元素到祖先元素冒泡。

If you change the return false; 如果更改则return false;否则, return false; to event.stopPropagation() instead, you don't prevent the default, and the links work. 改为event.stopPropagation() ,您无需阻止默认设置,并且链接可以正常工作。 (There's also no reason to return true from event handlers.) (也没有理由从事件处理程序中返回true 。)

Fiddle 小提琴

function settings_open(event){

    settings_canceltimer();
    settings_close();
    var submenu = $(this).find('ul');

    if(submenu){
        ddmenuitem = submenu.css('visibility', 'visible');
        event.stopPropagation(); // <== Change here
    }

    // Removed return true here
}

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

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