简体   繁体   English

悬停时的jQuery在导航菜单上显示子链接

[英]jQuery on hover show sub links on Navigation Menu

I have a navigation menu, when i hover over the links i want to show each links sub menu then toggle or hide and show each individual one. 我有一个导航菜单,当我将鼠标悬停在链接上时,我想显示每个链接子菜单,然后切换或隐藏并显示每个单独的菜单。

At the moment when I hover over the link on the navigation menu it displays all the sub menus for all the links. 当我将鼠标悬停在导航菜单上的链接上时,它会显示所有链接的所有子菜单。

I have attached a fiddle with a demo of my code so far: - 到目前为止,我已经在小提琴上附加了我的代码演示:-

http://jsfiddle.net/QTm2c/1/ http://jsfiddle.net/QTm2c/1/

Here is the jQuery 这是jQuery

$(document).ready(function() {
    $("li.navmain__item").hover(function () {
            $("span.navmain__item--subnavholder").toggle();
        })
})

Thanks 谢谢

Use : 采用 :

$(document).ready(function() {
    $("li.navmain__item").hover(function () {
        $(this).children("span.navmain__item--subnavholder").toggle();
    });
});

Fiddle 小提琴

Try this: 尝试这个:

$(document).ready(function() {
    $("li.navmain__item a").hover(function () {
        $(this).siblings().toggle();
    })
})

You have to target the specific submenu. 您必须定位特定的子菜单。 Inside the hover() handler, use: hover()处理函数中,使用:

$("span.navmain__item--subnavholder", this.parentElement).toggle();

You just have to get the subnavholder for the element you're hovering. 您只需要为要悬停的元素获取subnavholder。 In the callback, the element you're hovering over is stored in this (as an HTML Element). 在回调中,你将鼠标悬停在元素存储在this (作为一个HTML元素)。

Changing 改变中

$("span.navmain__item--subnavholder").toggle();

to

$(this).parent().find("span.navmain__item--subnavholder").toggle();

Will do the trick! 会成功的!

In your current setup this should do it 在您当前的设置中,应该这样做

$(document).ready(function() {
            $("li.navmain__item").hover(function () {
                $(this).find(".navmain__item--subnavholder").toggle();
            })
        })

You could also add indexing to the script. 您也可以在脚本中添加索引。 Here is edited, working jssfiddle: http://jsfiddle.net/QTm2c/8/ 此处已编辑,可以使用jssfiddle: http : //jsfiddle.net/QTm2c/8/

$(document).ready(function() {
        $("li.navmain__item a").hover(function () {
            var index = $( "li.navmain__item a" ).index( this );
            $("span.navmain__item--subnavholder"+index).toggle();
        })
    })

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

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