我遇到一个模板问题,即单击菜单项时,移动菜单不会折叠。试图解决此问题,添加了额外的类,额外的ID,但是仍然存在。仅当再次单击汉堡包图标时,它才会关闭/隐藏。这特别烦人...
导航栏html代码为:
<!-- Fixed navbar -->
<div class="navbar navbar-smak navbar-fixed-top" id="navbar" role="navigation">
<div class="container">
<div class="navbar-header">
<a class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse"><i class="fa fa-bars"></i></a>
<h1><a class="navbar-brand animate" href="#home">TEST SITE</a></h1>
</div>
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav navbar-right animate">
<li><a class="btn-navbar" href="#home">Home</a></li>
<li><a href="#blog">Blog</a></li>
<li><a href="#contact">Contacto</a></li>
</ul>
</div>
<!--/.nav-collapse -->
</div>
</div>
这是我正在处理的虚拟html文件,请随时下载并检查:https://aleare.com.ar/testing3/index2tst.html
这是处理折叠菜单的功能:
$('a[href*=#]').each(function () {
if (filterPath(location.pathname) == filterPath(this.pathname) && location.hostname == this.hostname && this.hash.replace(/#/, '')) {
var $targetId = $(this.hash),
$targetAnchor = $('[name=' + this.hash.slice(1) + ']');
var $target = $targetId.length ? $targetId : $targetAnchor.length ? $targetAnchor : false;
if ($target) {
$(this).click(function () {
//Hack collapse top navigation after clicking
topMenu.parent().attr('style', 'height:0px').removeClass('in'); //Close navigation
$('.navbar .btn-navbar').addClass('collapsed');
var targetOffset = $target.offset().top - 52;
$('html, body').animate({
scrollTop: targetOffset
}, 800);
return false;
});
}
}
});
有人知道为什么会失败吗?
答案 0 :(得分:0)
您还必须再加上一些标记,例如topMenu和filterPath。
但是,如果我正确理解了您的问题,这听起来像是您希望从移动菜单中选择一项时移动菜单关闭。我没有使用您的代码,而是从Bootstrap示例中借用了导航栏,并添加了一些额外的代码来在单击菜单中的链接时关闭菜单。如果引导程序中内置了类似的东西,我认为您正在使用,我不会感到惊讶。如果没有,您可以通过在“适当”时触发汉堡包的点击来执行类似的操作:
$('.nav-link:not(.dropdown-toggle), .dropdown-item').each(function () {
$(this).on("click", function(e) {
$(".navbar-toggler").trigger("click");
alert($(this).attr("href"));
});
});
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha256-YLGeXaapI0/5IgZopewRJcFXomhRMlYYjugPLSyNjTY=" crossorigin="anonymous" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.9.0/css/all.min.css" integrity="sha256-UzFD2WYH2U1dQpKDjjZK72VtPeWP50NoJjd26rnAdUI=" crossorigin="anonymous" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha256-CjSoeELFOcH0/uxWu6mC/Vlrc1AARqbm/jiiImDGV3s=" crossorigin="anonymous"></script>
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<a class="navbar-brand" href="#">Navbar</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav mr-auto">
<li class="nav-item active">
<a class="nav-link" href="#home">Home <span class="sr-only">(current)</span></a>
</li>
<li class="nav-item">
<a class="nav-link" href="#link">Link</a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Dropdown
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
<a class="dropdown-item" href="#action">Action</a>
<a class="dropdown-item" href="#another">Another action</a>
<div class="dropdown-divider"></div>
<a class="dropdown-item" href="#soemthingelse">Something else here</a>
</div>
</li>
<li class="nav-item">
<a class="nav-link disabled" href="#">Disabled</a>
</li>
</ul>
<form class="form-inline my-2 my-lg-0">
<input class="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search">
<button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
</form>
</div>
</nav>
<div class="col">
2 of 3
</div>
<div class="col">
3 of 3
</div>