简体   繁体   English

为什么显示===“ none”没有给出预期的结果

[英]Why does display === “none” not give result as expected

I have a Javascript function "display()". 我有一个Javascript函数“ display()”。 I what to make my nav display "none" on click, but is not working it just blinks 我该如何使我的导航在单击时显示“无”,但不起作用,它只是闪烁

I want to make it work using only javascript. 我想只使用javascript使其工作。 so i am not using queryselector 所以我没有使用queryselector

 function display() { var x = document.getElementById("nav-content"); x.style.display = "none"; } 
 <header> <div class="LiaWrapper clearfix"> <h1 id="logo">logo</h1> <a href="" id="drop-down-icon" onclick="display()"><i class="fa fa-bars" aria-hidden="true"></i> </a> <nav id="nav-content"> <ul> <li><a href="">Home</a></li> <li><a href="">services</a></li> <li><a href="">clients</a></li> <li><a href="">about us</a></li> <li><a href="">contact</a></li> </ul> </nav> </div> </header> 

I want to make my nav display none on click, but it just blinks on clicking 我想使我的导航在单击时不显示,但在单击时仅闪烁

This is happening because you are using onclick on <a> tag with href attribute as empty string. 之所以发生这种情况,是因为您在<a>标记上使用了onclick,并将href属性作为空字符串。

Try removing href attribute from the a tag 尝试从a标签中移除href属性

<a id="drop-down-icon" onclick="display()"><i class="fa fa-bars" aria-hidden="true"></ i> <a id="drop-down-icon" onclick="display()"><i class="fa fa-bars" aria-hidden="true"></ i>

Check the fiddle - https://jsfiddle.net/3xc6qfme/ 检查小提琴-https: //jsfiddle.net/3xc6qfme/

first remove the href attribute anchor tag is always used to redirect to certain url. 首先删除href属性标记始终用于重定向到某些url。 best practice is to use 最佳做法是使用

<input type="button" value="Click"/>

and then use your font awesome icons in place of value, but if you want to use anchor tag then remove href attribute or as href use 然后使用字体真棒图标代替值,但如果要使用锚标记,则删除href属性或作为href使用

javascript:void(0) 的javascript:无效(0)

then try 然后尝试

jquery: $("#id_name").css("display", "none"); jQuery:$(“#id_name”)。css(“ display”,“ none”);

javascript: document.getElementById("demo").style.display = "none"; javascript:document.getElementById(“ demo”)。style.display =“ none”;

Use this. 用这个。

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

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