jquery 下拉菜单
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了jquery 下拉菜单相关的知识,希望对你有一定的参考价值。
如图片显示
2个菜单鼠标移到上面就会显示下拉菜单,而且子菜单还可以超链接,尽量写详细点,不要用插件来实现,尽量加注释,才接触jquery
var timeoutId;
$(function ()
$("#menu li").each(function (index)
$(this).mouseover(function ()
timeoutId = setTimeout(function ()
//将所有样式为contentShow的移除
$("div.contentShow").removeClass("contentShow");
//将当前的显示
$("div.content:eq(" + index + ")").addClass("contentShow").css("margin-left", (index + 0.5) * 100 + "px");
, 300);
).mouseout(function ()
clearTimeout(timeoutId);
);
);
);
</script>
<style type="text/css">
div.contentclear:left;display:none;
div.contentShowdisplay:block;
atext-decoration:none;color:Black;
A:link COLOR: #00135b; text-decoration: none;
A:active COLOR: #00135b; text-decoration: none;
A:visited COLOR: #00135b; text-decoration: none;
A:hover COLOR: #ff8a00; text-decoration: none;
</style>
<div>
<ul id="menu" style="list-style:none;">
<li style=" float:left; margin:0px;cursor:pointer; width:100px; border:1px solid red;"> 一</li>
<li style=" float:left; margin:0px;cursor:pointer; width:100px; border:1px solid red;">二</li>
<li style=" float:left; margin:0px;cursor:pointer; width:100px; border:1px solid red;">三</li>
</ul>
<div style="margin-top:10px;">
<div class="content">
<ul style="list-style:none;">
<li style="width:100px; border:1px solid blue;"><a href="">一</a></li>
<li style="width:100px; border:1px solid blue;"><a href="">二</a></li>
<li style="width:100px; border:1px solid blue;"><a href="">三</a></li>
</ul>
</div>
<div class="content">
<ul style="list-style:none;">
<li style="width:100px; border:1px solid blue;"><a href="">df</a></li>
<li style="width:100px; border:1px solid blue;"><a href="">df</a></li>
<li style="width:100px; border:1px solid blue;"><a href="">df</a></li>
</ul>
</div>
<div class="content">
<ul style="list-style:none;">
<li style="width:100px; border:1px solid blue;"><a href="">43</a></li>
<li style="width:100px; border:1px solid blue;"><a href="">34</a></li>
<li style="width:100px; border:1px solid blue;"><a href="">34</a></li>
</ul>
</div>
</div>
</div> 参考技术A 效果是tab选取项卡的效果,要用js把它做出来。
<script language="javascript">
function tab(n)
for(i=0;i<3;i++)
document.getElementById("tab"+i).style.display="none";
documetn.getElementById("tab"+n).style.display="block";
</script>
<ul>
<li onmousemove="tab(0)">热点</li>
<li onmousemove="tab(1)">新闻</li>
<li onmousemove="tab(2)">活动</li>
</ul>
<div id="tab0" style="display:block;">
这里是热点模块内容
</div>
<div id="tab1" style="display:none;">
这里是新闻模块内容
</div>
<div id="tab2" style="display:none;">
这里是活动模块内容
</div>
Jquery 没有关闭下拉菜单
【中文标题】Jquery 没有关闭下拉菜单【英文标题】:Jquery is not closing drop-down 【发布时间】:2021-05-11 12:39:55 【问题描述】:我正在使用 Bootstrap 导航栏构建多级(仅第二级)下拉菜单。我可以找到一个guide 来构建多级下拉菜单。
JSFiddle link
但是,下拉菜单不会在点击时关闭。点击Document
可以关闭下拉菜单,我在jquery
下面添加了。
$(document).click(function()
$('.dropdown-menu').removeClass('show');
);
下拉菜单关闭,但在其他nav-item
中单击时,打开的下拉菜单不会关闭。所以,我缺少基本的JQuery
。
基本上show
类在单击其他nav-item
时不会从.dropdown-menu
类中删除。所以,我想从.dropdown-menu
中删除show
类
编辑:我正在寻找工作 JS
或 Jquery
代码,上面的 Jquery
是从 staoverflow 复制的损坏代码。所以,寻找完整和干净的解决方案
$('.dropdown-menu a.dropdown-toggle').on('click', function(e)
if (!$(this).next().hasClass('show'))
$(this).parents('.dropdown-menu').first().find('.show').removeClass('show');
var $subMenu = $(this).next('.dropdown-menu');
$subMenu.toggleClass('show');
return false;
);
$(document).click(function()
// $('.dropdown-menu').toggleClass('show');
$('.dropdown-menu').removeClass('show');
);
$('.dropdown').click(function(event)
event.stopPropagation();
);
.dropdown-submenu
position: relative;
.dropdown-submenu a::after
transform: rotate(-90deg);
position: absolute;
right: 6px;
top: .8em;
.dropdown-submenu .dropdown-menu
top: 0;
left: 100%;
margin-left: .1rem;
margin-right: .1rem;
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-giJF6kkoqNQ00vy+HMDP7azOuL0xtbfIcaT9wjKHr8RbDVddVHyTfAAsrekwKmP1" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/js/bootstrap.bundle.min.js" integrity="sha384-ygbV9kiqUc6oa4msXn9868pTtWMgiQaeYH7/t7LECLbyPA2x65Kgf80OJFdroafW" crossorigin="anonymous"></script>
<nav class="navbar navbar-expand-md navbar-dark bg-primary py-1">
<div class="container-fluid">
<div class="flex-row d-flex">
<a class="navbar-brand" href="#">Brand</a>
</div>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="navbar-collapse collapse" id="navbarSupportedContent">
<ul class="navbar-nav">
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" role="button" data-bs-toggle="dropdown" aria-expanded="false">
Dogs
</a>
<ul class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
<li><a class="dropdown-item" href="#">Bulldog</a></li>
<li><a class="dropdown-item" href="#">Al</a></li>
<li><a class="dropdown-item" href="#">Labrador</a></li>
<li class="dropdown-submenu"><a class="dropdown-item dropdown-toggle" href="#">Puppies</a>
<ul class="dropdown-menu">
<li><a class="dropdown-item" href="#">Husky</a></li>
<li><a class="dropdown-item" href="#">Husky B</a></li>
<li><a class="dropdown-item" href="#">Husky C</a></li>
</ul>
</li>
<li class="dropdown-submenu"><a class="dropdown-item dropdown-toggle" href="#">Pet Products</a>
<ul class="dropdown-menu">
<li><a class="dropdown-item" href="#">Cat</a></li>
<li><a class="dropdown-item" href="#">Dogs</a></li>
<li><a class="dropdown-item" href="#">Reptile</a></li>
<li><a class="dropdown-item" href="#">Amphibian</a></li>
</ul>
</li>
</ul>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" role="button" data-bs-toggle="dropdown" aria-expanded="false">
Pet Cages
</a>
<ul class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
<li><a class="dropdown-item" href="#">Cat</a></li>
<li><a class="dropdown-item" href="#">Dogs</a></li>
<li><a class="dropdown-item" href="#">Reptile</a></li>
</ul>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" role="button" data-bs-toggle="dropdown" aria-expanded="false">
Pet Guides
</a>
<ul class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
<li><a class="dropdown-item" href="#">Labrador</a></li>
<li><a class="dropdown-item" href="#">Cat</a></li>
<li><a class="dropdown-item" href="#">Lizard</a></li>
</ul>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" role="button" data-bs-toggle="dropdown" aria-expanded="false">
ML & AI
</a>
<ul class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
<li><a class="dropdown-item" href="#">Pet CBD</a></li>
</ul>
</li>
<!-- <form class="d-flex"> -->
<input class="form-control me-2" type="search" placeholder="Search" aria-label="Search">
<!-- <button class="btn btn-outline-success" type="submit">Search</button> -->
<!-- </form> -->
</ul>
</div>
</div>
</nav>
【问题讨论】:
@user1735921 在父元素#navbarSupportedContent
的类列表中添加show
当我点击其他导航项时它会为我关闭。
如果您不使用其他编辑器,请在 JSFiddle 中尝试
@munna01 您在问题中的完整示例代码集应该完全重现您的问题。在 Chrome 中进行测试,这个 sn-p 似乎可以正常工作。请在该特定代码集中提供有关您的问题的更多详细信息或特定信息,作为您实际问题的一部分。 (不是小提琴,这里是这个)
@MarkSchultheiss 请检查,我已经添加了 gif 截图(解释问题 - “狗下拉菜单应该关闭点击宠物笼下拉菜单”
【参考方案1】:
对于 持久性打开的 DROPWON 的主要问题,您可以通过侦听 nav-link
事件 show.bs.dropdown
来解决该问题,然后从 .dropdown
包装的 .dropdown-submenu
中删除 show 类
如下:
$('.nav-link').on('show.bs.dropdown', function(e)
$(".dropdown-submenu .dropdown-menu").removeClass("show");
);
请参阅下面的工作 sn-p :(也可以全屏尝试)
为了测试,我从 sn-p 中删除了一些菜单:
$('.dropdown-menu a.dropdown-toggle').on('click', function(e)
if (!$(this).next().hasClass('show'))
$(this).parents('.dropdown-menu').first().find('.show').removeClass('show');
var $subMenu = $(this).next('.dropdown-menu');
$subMenu.toggleClass('show');
return false;
);
$('.nav-link').on('show.bs.dropdown', function(e)
$(".dropdown-submenu .dropdown-menu").removeClass("show");
);
$(document).on("click", ".nav-link", function(e)
console.log("clicl nav");
)
$('.dropdown').click(function(event)
console.log("click");
event.stopPropagation();
);
.dropdown-submenu
position: relative;
.dropdown-submenu a::after
transform: rotate(-90deg);
position: absolute;
right: 6px;
top: .8em;
.dropdown-submenu .dropdown-menu
top: 0;
left: 100%;
margin-left: .1rem;
margin-right: .1rem;
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/js/bootstrap.bundle.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/css/bootstrap.min.css" rel="stylesheet" />
<nav class="navbar navbar-expand-md navbar-dark bg-primary py-1">
<div class="container-fluid">
<div class="flex-row d-flex">
<a class="navbar-brand" href="#">Brand</a>
</div>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="navbar-collapse collapse" id="navbarSupportedContent">
<ul class="navbar-nav">
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" role="button" data-bs-toggle="dropdown" aria-expanded="false">
Dogs
</a>
<ul class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
<li><a class="dropdown-item" href="#">Bulldog</a></li>
<li><a class="dropdown-item" href="#">Al</a></li>
<li><a class="dropdown-item" href="#">Labrador</a></li>
<li class="dropdown-submenu"><a class="dropdown-item dropdown-toggle" href="#">Puppies</a>
<ul class="dropdown-menu">
<li><a class="dropdown-item" href="#">Husky</a></li>
<li><a class="dropdown-item" href="#">Husky B</a></li>
<li><a class="dropdown-item" href="#">Husky C</a></li>
</ul>
</li>
<li class="dropdown-submenu"><a class="dropdown-item dropdown-toggle" href="#">Pet Products</a>
<ul class="dropdown-menu">
<li><a class="dropdown-item" href="#">Cat</a></li>
<li><a class="dropdown-item" href="#">Dogs</a></li>
<li><a class="dropdown-item" href="#">Reptile</a></li>
<li><a class="dropdown-item" href="#">Amphibian</a></li>
</ul>
</li>
</ul>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" role="button" data-bs-toggle="dropdown" aria-expanded="false">
Pet Cages
</a>
<ul class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
<li><a class="dropdown-item" href="#">Cat</a></li>
<li><a class="dropdown-item" href="#">Dogs</a></li>
<li><a class="dropdown-item" href="#">Reptile</a></li>
</ul>
</li>
<!-- <button class="btn btn-outline-success" type="submit">Search</button> -->
<!-- </form> -->
</ul>
</div>
</div>
</nav>
【讨论】:
@munna01,你能准确描述一下你需要什么吗?单击外部时关闭下拉菜单? 是的。点击甚至其他导航链接,应该关闭以前打开的导航(side-nav)s 能否请您调查一下 - 这个问题,提出了新问题:***.com/questions/66098848/… @munna01 ,更新了我的答案,我想现在我可以理解你的问题了(在看到屏幕记录 gif 之后) 是我的错误,我不能写得更好来说服你。可能是我英语不好(是这个原因)【参考方案2】:您无需添加jQuery
即可实现此目的。
删除jQuery
和额外的javascript
代码,您的下拉菜单将通过添加一个简单的css
代码来工作。
.dropdown-submenu
position: relative;
.dropdown-submenu > .dropdown-menu
top: 0;
left: 100%;
margin-top: -6px;
margin-left: -1px;
-webkit-border-radius: 0 6px 6px 6px;
-moz-border-radius: 0 6px 6px;
border-radius: 0 6px 6px 6px;
.dropdown-submenu:hover > .dropdown-menu
display: block;
.dropdown-submenu>a : after
display: block;
content: " ";
float: right;
width: 0;
height: 0;
border-color: transparent;
border-style: solid;
border-width: 5px 0 5px 5px;
border-left-color: #ccc;
margin-top: 5px;
margin-right: -10px;
.dropdown-submenu:hover > a : after
border-left-color: #fff;
.dropdown-submenu.pull-left
float: none;
.dropdown-submenu.pull-left > .dropdown-menu
left: -100%;
margin-left: 10px;
-webkit-border-radius: 6px 0 6px 6px;
-moz-border-radius: 6px 0 6px 6px;
border-radius: 6px 0 6px 6px;
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-giJF6kkoqNQ00vy+HMDP7azOuL0xtbfIcaT9wjKHr8RbDVddVHyTfAAsrekwKmP1" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/js/bootstrap.bundle.min.js" integrity="sha384-ygbV9kiqUc6oa4msXn9868pTtWMgiQaeYH7/t7LECLbyPA2x65Kgf80OJFdroafW" crossorigin="anonymous"></script>
<nav class="navbar navbar-expand-md navbar-dark bg-primary py-1">
<div class="container-fluid">
<div class="flex-row d-flex">
<a class="navbar-brand" href="#">Brand</a>
</div>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="navbar-collapse collapse" id="navbarSupportedContent">
<ul class="navbar-nav">
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" role="button" data-bs-toggle="dropdown" aria-expanded="false">
Dogs
</a>
<ul class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
<li><a class="dropdown-item" href="#">Bulldog</a></li>
<li><a class="dropdown-item" href="#">Al</a></li>
<li><a class="dropdown-item" href="#">Labrador</a></li>
<li class="dropdown-submenu"><a class="dropdown-item dropdown-toggle" href="#">Puppies</a>
<ul class="dropdown-menu">
<li><a class="dropdown-item" href="#">Husky</a></li>
<li><a class="dropdown-item" href="#">Husky B</a></li>
<li><a class="dropdown-item" href="#">Husky C</a></li>
</ul>
</li>
<li class="dropdown-submenu"><a class="dropdown-item dropdown-toggle" href="#">Pet Products</a>
<ul class="dropdown-menu">
<li><a class="dropdown-item" href="#">Cat</a></li>
<li><a class="dropdown-item" href="#">Dogs</a></li>
<li><a class="dropdown-item" href="#">Reptile</a></li>
<li><a class="dropdown-item" href="#">Amphibian</a></li>
</ul>
</li>
</ul>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" role="button" data-bs-toggle="dropdown" aria-expanded="false">
Pet Cages
</a>
<ul class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
<li><a class="dropdown-item" href="#">Cat</a></li>
<li><a class="dropdown-item" href="#">Dogs</a></li>
<li><a class="dropdown-item" href="#">Reptile</a></li>
</ul>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" role="button" data-bs-toggle="dropdown" aria-expanded="false">
Pet Guides
</a>
<ul class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
<li><a class="dropdown-item" href="#">Labrador</a></li>
<li><a class="dropdown-item" href="#">Cat</a></li>
<li><a class="dropdown-item" href="#">Lizard</a></li>
</ul>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" role="button" data-bs-toggle="dropdown" aria-expanded="false">
ML & AI
</a>
<ul class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
<li><a class="dropdown-item" href="#">Pet CBD</a></li>
</ul>
</li>
<!-- <form class="d-flex"> -->
<input class="form-control me-2" type="search" placeholder="Search" aria-label="Search">
<!-- <button class="btn btn-outline-success" type="submit">Search</button> -->
<!-- </form> -->
</ul>
</div>
</div>
</nav>
【讨论】:
不能接受悬停,这会破坏移动网站。以上是关于jquery 下拉菜单的主要内容,如果未能解决你的问题,请参考以下文章