简体   繁体   English

如何在display:none和display:block之间切换

[英]How to toggle between display:none and display:block

Responsive menu 响应式菜单

I've got a navigation menu that holds 4 elements. 我有一个包含4个元素的导航菜单。 One of them is not displayed (display:none). 不显示其中之一(显示:无)。 When you resize a window to 700px navigation menu appears and 3 other elements become hidden. 当您将窗口调整为700像素时,将显示导航菜单,其他3个元素变为隐藏状态。 When you click on "Menu" JQuery selects all hidden elements and display them as block elements. 当您单击“菜单”时,JQuery将选择所有隐藏的元素并将其显示为块元素。 But when you click on "Menu" second time nothing happens. 但是,当您第二次单击“菜单”时,什么也没有发生。 Can't figure out how to fix JQuery code. 无法弄清楚如何修复JQuery代码。 Can anyone help? 有人可以帮忙吗?

HTML: HTML:

<ul class="topnav">
    <a href="javascript:void(0);" onclick="myFunction()"><li class="menu">Menu</li></a>
    <li>test 1</li>
    <li>test 2</li>
    <li>test 3</li>
</ul>

CSS: CSS:

    .topnav {
        max-width: 900px;
        margin: 0 auto;
        text-align: center;
    }

    .topnav li {
        display: inline-block;
        padding: 10px;
    }

    li.menu {
        display: none;
    }

@media screen and (max-width:700px) {

    .topnav li {
        display: none;
    }

    li.menu {
        display: block;
    }   
}

JQuery: jQuery的:

function myFunction() {

        if($("ul.topnav li:hidden")) {
            $("ul.topnav li:hidden").css("display", "block");
        }
        else  {
            $("ul.topnav li:not(:first-child)").css("display", "none");
        }

    }

Passing a selector to jQuery in an expression like $("ul.topnav li:hidden") will always result in a jQuery object being returned. $("ul.topnav li:hidden")这样的表达式中将选择器传递给jQuery 总是会导致返回jQuery对象。 Thus, 从而,

    if($("ul.topnav li:hidden")) {

will always succeed, because an object reference is always "truthy". 始终成功,因为对象引用始终是“真实的”。 If you want to test whether the selector matched any elements: 如果要测试选择器是否匹配任何元素:

    if($("ul.topnav li:hidden").length) {

will check for the length to be non-zero. 将检查长度是否为非零。

edit and as pointed out in a comment to my answer, the jQuery .toggle() method may suit your needs and make your code simpler and shorter. 编辑,并在对我的答案的评论中指出,jQuery .toggle()方法可能适合您的需求,并使您的代码更简单,更短。

You never need to use an attribute event handler if you are using jQuery. 如果您使用的是jQuery,则无需使用属性事件处理程序。 There's several ways to achieve your objective. 有几种方法可以实现您的目标。 .toggle() is probably the easiest method to use. .toggle()可能是最容易使用的方法。 Btw, didn't understand the layout you had a <li> inside an <a> so I rearranged elements a bit so it's efficient. 顺便说一句,我不理解在<a>内有<li>的布局,所以我对元素进行了一些重新排列以提高效率。

SNIPPET SNIPPET

 $('.menu').on('click', function(e) { e.preventDefault(); $('.topnav li').toggle(); }); 
 .topnav { max-width: 900px; margin: 0 auto; text-align: center; } .topnav li { display: inline-block; padding: 10px; } .menu { display: none; } @media screen and (max-width: 700px) { .topnav li { display: none; } .menu { display: block; } } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <a href="#/" class='menu'>Menu</a> <ul class="topnav"> <li>test 1</li> <li>test 2</li> <li>test 3</li> </ul> 

First off, you can't have an <a> tag as a direct descendant of a <ul> . 首先,您不能将<a>标记作为<ul>的直接后代。

With jQuery, it's also better practiced to hook onto a click event with jQuery, rather than an inline onClick handler. 使用jQuery时,最好使用 jQuery而不是内联onClick处理程序来钩住click事件。

Lastly it would be better to apply a class to your parent <ul> element to control the display property of it's children, as opposed to applying inline styles to your list items. 最后,最好将一个类应用于您的父<ul>元素,以控制其子元素的显示属性,而不是将内联样式应用于您的列表项。

Refactored code: https://jsfiddle.net/00wdb9q6/1/ 重构代码: https : //jsfiddle.net/00wdb9q6/1/

$('li.menu').on('click',function(){
  $(this).parent().toggleClass('show-items');
});

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

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