我的 JQuery 导航出了什么问题?

发布于 2024-12-10 00:40:14 字数 3838 浏览 0 评论 0原文

目前,我的网站有一个下拉菜单设置,如下所示:

HTML:

<ul class="primary-navigation">
    <li><a href="/About" id="about"><span>About</span></a></li>
    <li><a href="/Location" id="location"><span>Location</span></a></li>
    <li><a href="/Site-Plan"><span>Site Plan</span></a></li>
    <li><a href="/Specification"><span>Specification</span></a></li>
    <li><a href="/Gallery"><span>Gallery</span></a></li>
    <li><a href="/Investors"><span>Investors</span></a></li>
    <li class="last"><a href="/Contact"><span>Contact</span></a></li>
</ul>
<ul class="secondary-navigation" id="about-menu">
    <li><a href="/Item1">Item1</a></li>
    <li><a href="/Item2">Item2</a></li>
</ul>   
<ul class="secondary-navigation" id="location-menu">
    <li><a href="/Item1">Amenities</a></li>
    <li><a href="/Item2">Connections</a></li>
    <li><a href="/Item3>Location Map</a></li>
</ul>     

Javascript:

$(document).ready(function () {
    // About
    $("#about").hover(
        function() {
            $("#about-menu").css("display","block");
        },
        function() {
            $("#about-menu").css("display","none");
        }
    );
    $("#about-menu").hover(
        function () {
            $(this).css("display", "block");
        },
        function () {
            $(this).css("display", "none");
        }
    );
    $("#about-menu li").hover(
        function () {
            $("#about-menu").css("display", "block");
        },
        function () {
            $("#about-menu").css("display", "none");
        }
    );
    // Location
    $("#location").hover(
        function () {
            $("#location-menu").css("display", "block");
        },
        function () {
            $("#location-menu").css("display", "none");
        }
    );
    $("#location-menu").hover(
        function () {
            $(this).css("display", "block");
        },
        function () {
            $(this).css("display", "none");
        }
    );
});

CSS:

#header ul.primary-navigation { width: 735px; height: 90px; overflow: auto; position:            absolute; top: 0px; right: 0px; background: #FFFFFF; }
#header ul.primary-navigation li { list-style-type: none; width: 104px; height: 89px;    float: left; position: relative; border-right: 1px solid #FFFFFF; background: #b3b3b3; }
#header ul.primary-navigation li:hover { background: #adcc52; }
#header ul.primary-navigation li.last { border-right: 1px solid #b3b3b3; }
#header ul.primary-navigation li.last:hover { background: #adcc52; border-right: 1px solid #adcc52; }
#header ul.primary-navigation li a { display: block; width: 104px; height: 89px; text-decoration: none; }
#header ul.primary-navigation li a span { color: #FFFFFF; display: block; position: absolute; left: 10px; bottom: 10px; }

#header ul.secondary-navigation { width: 735px; display: none; position: absolute; top:  89px; left: 225px; background: red; overflow: auto; }  
#header ul.secondary-navigation li { width: 735px; height: 30px; position: relative; float: left; /*background: #adcc52;*/ }

导航似乎首先工作。然而,似乎每当子菜单处于活动状态时,如果您尝试将鼠标悬停在第一个

  • 项目之后的任何内容上,子导航就会消失。我一生都无法弄清楚为什么会发生这种事!您可能会认为,由于
  • 项嵌套在父
      中,因此它们都会显示,直到您移出 >
        但它似乎不是这样工作的。
  • 有谁知道为什么会发生这种情况?

    谢谢!

    Currently I have a drop down menu setup for my website as follows:

    HTML:

    <ul class="primary-navigation">
        <li><a href="/About" id="about"><span>About</span></a></li>
        <li><a href="/Location" id="location"><span>Location</span></a></li>
        <li><a href="/Site-Plan"><span>Site Plan</span></a></li>
        <li><a href="/Specification"><span>Specification</span></a></li>
        <li><a href="/Gallery"><span>Gallery</span></a></li>
        <li><a href="/Investors"><span>Investors</span></a></li>
        <li class="last"><a href="/Contact"><span>Contact</span></a></li>
    </ul>
    <ul class="secondary-navigation" id="about-menu">
        <li><a href="/Item1">Item1</a></li>
        <li><a href="/Item2">Item2</a></li>
    </ul>   
    <ul class="secondary-navigation" id="location-menu">
        <li><a href="/Item1">Amenities</a></li>
        <li><a href="/Item2">Connections</a></li>
        <li><a href="/Item3>Location Map</a></li>
    </ul>     
    

    Javascript:

    $(document).ready(function () {
        // About
        $("#about").hover(
            function() {
                $("#about-menu").css("display","block");
            },
            function() {
                $("#about-menu").css("display","none");
            }
        );
        $("#about-menu").hover(
            function () {
                $(this).css("display", "block");
            },
            function () {
                $(this).css("display", "none");
            }
        );
        $("#about-menu li").hover(
            function () {
                $("#about-menu").css("display", "block");
            },
            function () {
                $("#about-menu").css("display", "none");
            }
        );
        // Location
        $("#location").hover(
            function () {
                $("#location-menu").css("display", "block");
            },
            function () {
                $("#location-menu").css("display", "none");
            }
        );
        $("#location-menu").hover(
            function () {
                $(this).css("display", "block");
            },
            function () {
                $(this).css("display", "none");
            }
        );
    });
    

    CSS:

    #header ul.primary-navigation { width: 735px; height: 90px; overflow: auto; position:            absolute; top: 0px; right: 0px; background: #FFFFFF; }
    #header ul.primary-navigation li { list-style-type: none; width: 104px; height: 89px;    float: left; position: relative; border-right: 1px solid #FFFFFF; background: #b3b3b3; }
    #header ul.primary-navigation li:hover { background: #adcc52; }
    #header ul.primary-navigation li.last { border-right: 1px solid #b3b3b3; }
    #header ul.primary-navigation li.last:hover { background: #adcc52; border-right: 1px solid #adcc52; }
    #header ul.primary-navigation li a { display: block; width: 104px; height: 89px; text-decoration: none; }
    #header ul.primary-navigation li a span { color: #FFFFFF; display: block; position: absolute; left: 10px; bottom: 10px; }
    
    #header ul.secondary-navigation { width: 735px; display: none; position: absolute; top:  89px; left: 225px; background: red; overflow: auto; }  
    #header ul.secondary-navigation li { width: 735px; height: 30px; position: relative; float: left; /*background: #adcc52;*/ }
    

    The navigation appears to be working at first. However it seems that whenever a submenu is active, if you try to hover over anything after the first <li> item, the sub nav disappears. I can't for the life of me figure out why this is happening! You'd think that because the <li> items are nested within a parent <ul>, that they'd all be displaying until you move off the <ul> but it doesn't seem to work that way.

    Does anyone know why this is happening?

    Thanks!

    如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

    扫码二维码加入Web技术交流群

    发布评论

    需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

    评论(1

    胡渣熟男 2024-12-17 00:40:14

    我想说的是,

    $("#about-menu li").hover(
        function () {
            $("#about-menu").css("display", "block");
        },
        function () {
            $("#about-menu").css("display", "none");
        }
    );
    

    当你离开 LI 时,第二个回调被调用,整个菜单消失。更多 LI 不会显示,也无法触发菜单再次显示。您需要删除此回调。

    另一个技巧,不用 css("display...),只需使用 .show().hide()

    I would say that in

    $("#about-menu li").hover(
        function () {
            $("#about-menu").css("display", "block");
        },
        function () {
            $("#about-menu").css("display", "none");
        }
    );
    

    When you leave an LI, the second callback is called and the whole menu disappears. Further LIs are not shown and cannot trigger the menu to be shown again. You need to remove this callback.

    Another trick, instead of css("display..., just use .show() and .hide()

    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文