简体   繁体   English

锚标签在div内不起作用

[英]anchor tag not working inside a div

I am creating a navbar and following bootstrap styling. 我正在创建一个导航栏,并遵循引导样式。 My anchor tag is not working on the drop-down nav menus. 我的锚标记在下拉导航菜单上不起作用。 I tried many examples of similar problems people were having. 我尝试了许多人们遇到类似问题的例子。 like z-index, block element, floating. 像z-index,块元素,浮动。 None of those seems to work in my case. 在我看来,这些都不起作用。 Not sure whats going on.Below is the partial code i am working with. 不知道发生了什么。下面是我正在使用的部分代码。 I have a jquery function that I use to detect the mouse hover so I can drop the menu and other things. 我有一个jquery函数,用于检测鼠标悬停,因此可以删除菜单和其他内容。 there i do have event prevent default. 那里我确实有事件阻止默认值。 But removing that doesnt seem to do anything either 但是删除它似乎也没有任何作用

 var acc = document.getElementsByClassName("accordion"); var i; for (i = 0; i < acc.length; i++) { acc[i].onclick = function () { this.classList.toggle("active"); var panel = this.nextElementSibling; if (panel.style.maxHeight) { panel.style.maxHeight = null; } else { panel.style.maxHeight = panel.scrollHeight + "px"; } } } $(document).ready(function () { $(' .dropdown-content a').click(function (e) { e.preventDefault(); $(' .dropdown-content a').removeClass('active1'); $(this).closest(' .dropdown li').find("a:eq(0)").addClass('active1'); $(this).closest(' .top-nav li').find("a:eq(0)").addClass('dropbtn'); $(this).addClass('active1'); }); }); 
 .navbar-inverse{background-color:#004f8e!important; border-color: #004f8e!important; border-radius:0px!important; border-top: 6px solid #57be17!important;} .top-nav li a{color:white!important;} .top-nav li ai{ font-size: 9px; vertical-align: middle;margin-top: -3px; margin-right: 12px;} .dropbtn {color: white; padding: 16px; font-size: 16px;border: none;cursor: pointer;} .dropdown { position: relative; display: inline-block;} .dropdown-content { display: none; position: absolute; background-color: #f9f9f9;min-width: 190px; box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2); z-index: 5;} .dropdown-content a {color: black;padding: 12px 16px;text-decoration: none;display: block; z-index: 15;} .dropdown:hover .dropdown-content { display: block;} .dropdown:hover .dropbtn { text-decoration:none; background-color:#57be17;} .active1{background:#57be17;} a{ text-decoration:none!important;} 
  <div class="collapse navbar-collapse" id="myNavbar"> <ul class="nav navbar-nav top-nav pull-right"> <div class="dropdown"> <li class="dropbtn"><a href="#"><i class="fa fa-chevron-down" aria-hidden="true"></i> About Us</a></li> <div class="dropdown-content"> <a href="https://www.yahoo.com/">Action</a> <a href="#">Another action</a> <a href="#">Something else here</a> <a href="#">Separated link</a> <a href="#">One more separated link</a> </div> </div> <div class="dropdown"> <li class="dropbtn"><a href="#"><i class="fa fa-chevron-down" aria-hidden="true"></i> Providers</a></li> <div class="dropdown-content"> <a href="#">Action</a> <a href="#">Another action</a> <a href="#">Something else here</a> <a href="#">Separated link</a> <a href="#">One more separated link</a> </div> </div> <div class="dropdown"> <li class="dropbtn"><a href="#"><i class="fa fa-chevron-down" aria-hidden="true"></i> Payors</a></li> <div class="dropdown-content"> <a href="#">Action</a> <a href="#">Another action</a> <a href="#">Something else here</a> <a href="#">Separated link</a> <a href="#">One more separated link</a> </div> </div> <div class="dropdown"> <li class="dropbtn"><a href="#"><i class="fa fa-chevron-down" aria-hidden="true"></i> Employers</a></li> <div class="dropdown-content"> <a href="#">Action</a> <a href="#">Another action</a> <a href="#">Something else here</a> <a href="#">Separated link</a> <a href="#">One more separated link</a> </div> </div> <div class="dropdown"> <li class="dropbtn"><a href="#"><i class="fa fa-chevron-down" aria-hidden="true"></i> Patients</a></li> <div class="dropdown-content"> <a href="#">Action</a> <a href="#">Another action</a> <a href="#">Something else here</a> <a href="#">Separated link</a> <a href="#">One more separated link</a> </div> </div> </ul> </div> 

Remove e.preventDefault(); 删除e.preventDefault(); from the anchor click call. 从定位click呼叫。 It stops the default action of an element from happening. 它阻止发生元素的默认操作。 In case of link, it stops the link from opening the URL. 如果是链接,它将阻止链接打开URL。 Moreover, most of your links do not have a href specified. 此外,您的大多数链接都没有指定href。 Where will the anchor tag navigate to? 定位标记将导航到哪里?

You need to include JQuery. 您需要包括JQuery。 I adjusted the following snippet so you could see the outline of the a tag to hover over. 我调整了以下代码段,以便您可以看到要悬停的代码的轮廓。

If you're working on a JQuery project and you see the phrase $ is undefined it means that the JQuery library either hasn't been included, has been overwritten, or somewhere in the code you've used JQuery.noConflict() - which allows you to use JQuery objects by using JQuery instead of $ . 如果您正在处理JQuery项目,并且看到短语$ is undefined则意味着JQuery库尚未包括,已被覆盖或在您使用过JQuery.noConflict()的代码中-允许您通过使用JQuery而不是$使用JQuery对象。 More often than not it's because it wasn't included. 通常是因为不包含在内。

 var acc = document.getElementsByClassName("accordion"); var i; for (i = 0; i < acc.length; i++) { acc[i].onclick = function () { this.classList.toggle("active"); var panel = this.nextElementSibling; if (panel.style.maxHeight) { panel.style.maxHeight = null; } else { panel.style.maxHeight = panel.scrollHeight + "px"; } } } $(document).ready(function () { $(' .dropdown-content a').click(function (e) { e.preventDefault(); $(' .dropdown-content a').removeClass('active1'); $(this).closest(' .dropdown li').find("a:eq(0)").addClass('active1'); $(this).closest(' .top-nav li').find("a:eq(0)").addClass('dropbtn'); $(this).addClass('active1'); }); }); 
 .navbar-inverse{background-color:#004f8e!important; border-color: #004f8e!important; border-radius:0px!important; border-top: 6px solid #57be17!important;} .top-nav li a{color:white!important;} .top-nav li ai{ font-size: 9px; vertical-align: middle;margin-top: -3px; margin-right: 12px;} .dropbtn {color: white; padding: 16px; font-size: 16px;border: none;cursor: pointer;} .dropdown { position: relative; display: inline-block;} .dropdown-content { display: none; position: absolute; background-color: #f9f9f9;min-width: 190px; box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2); z-index: 5;} .dropdown-content a {color: black;padding: 12px 16px;text-decoration: none;display: block; z-index: 15;} .dropdown:hover .dropdown-content { display: block;} .dropdown:hover .dropbtn { text-decoration:none; background-color:#57be17;} .active1{background:#57be17;} a{ text-decoration:none!important; border: 1px black solid;} 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="collapse navbar-collapse" id="myNavbar"> <ul class="nav navbar-nav top-nav pull-right"> <div class="dropdown"> <li class="dropbtn"><a href="#"><i class="fa fa-chevron-down" aria-hidden="true"></i> About Us</a></li> <div class="dropdown-content"> <a href="https://www.yahoo.com/">Action</a> <a href="#">Another action</a> <a href="#">Something else here</a> <a href="#">Separated link</a> <a href="#">One more separated link</a> </div> </div> <div class="dropdown"> <li class="dropbtn"><a href="#"><i class="fa fa-chevron-down" aria-hidden="true"></i> Providers</a></li> <div class="dropdown-content"> <a href="#">Action</a> <a href="#">Another action</a> <a href="#">Something else here</a> <a href="#">Separated link</a> <a href="#">One more separated link</a> </div> </div> <div class="dropdown"> <li class="dropbtn"><a href="#"><i class="fa fa-chevron-down" aria-hidden="true"></i> Payors</a></li> <div class="dropdown-content"> <a href="#">Action</a> <a href="#">Another action</a> <a href="#">Something else here</a> <a href="#">Separated link</a> <a href="#">One more separated link</a> </div> </div> <div class="dropdown"> <li class="dropbtn"><a href="#"><i class="fa fa-chevron-down" aria-hidden="true"></i> Employers</a></li> <div class="dropdown-content"> <a href="#">Action</a> <a href="#">Another action</a> <a href="#">Something else here</a> <a href="#">Separated link</a> <a href="#">One more separated link</a> </div> </div> <div class="dropdown"> <li class="dropbtn"><a href="#"><i class="fa fa-chevron-down" aria-hidden="true"></i> Patients</a></li> <div class="dropdown-content"> <a href="#">Action</a> <a href="#">Another action</a> <a href="#">Something else here</a> <a href="#">Separated link</a> <a href="#">One more separated link</a> </div> </div> </ul> </div> 

it may be because jQuery is being sourced after the DOM is ready. 可能是因为DOM准​​备就绪后才生成jQuery。 Try wrapping your jQuery code in a setTimeout(function () { }, 3000) to get it to wait 3 seconds and see if jQuery becomes visible, if not visible at DOM ready. 尝试将jQuery代码包装在setTimeout(function(){},3000)中,使其等待3秒钟,看看jQuery是否变为可见(如果在DOM准备就绪时不可见)。

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

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