简体   繁体   English

如何做将改变元素样式显示的Javascript函数?

[英]How to do Javascript Function which will change the style display of element?

function burgerMenu(){
            //alert(document.getElementById("hiddenMenuUL").style.display);
            if(document.getElementById("hiddenMenuUL").style.display="none"){
                document.getElementById("hiddenMenuUL").style.display="block";
            }
            else if(document.getElementById("hiddenMenuUL").style.display="block"){
                document.getElementById("hiddenMenuUL").style.display="none";
            }
    }

I want to toggle the burger menu icon.我想切换汉堡菜单图标。 So if I press the icon the burgerMenu() function will be toggle and verify if its display: none then change to display:block and vice versa.因此,如果我按下图标, burgerMenu() 函数将被切换并验证其 display: none 然后更改为 display:block ,反之亦然。 hiddenMenuUL is the for list of links. hiddenMenuUL 是链接列表。 This line below is the button line in HTML:下面这一行是 HTML 中的按钮行:

<button class="btn" onclick="burgerMenu()">&#9776;</button>

My CSS for hiddenMenuUL我的 hiddenMenuUL 的 CSS

#hiddenMenuUL{
        display: none;
        list-style: none;
        width: 100%;
        justify-content: center;
    }

When you are writing your if, you are not actually checking if the display === "none", you are trying to set it to none.当您编写 if 时,您实际上并没有检查 display === "none" 是否显示,而是尝试将其设置为 none。

What you should be doing looks like this:你应该做的事情是这样的:

function burgerMenu(){
            let menuStyle = document.getElementById("hiddenMenuUL").style.display;
            if(menuStyle === 'none'){
                document.getElementById("hiddenMenuUL").style.display="block";
            }
            else {
                document.getElementById("hiddenMenuUL").style.display="none";
            }
    }

Edit:编辑:

It seems display rule is defined in CSS, and isn't inline.似乎显示规则是在 CSS 中定义的,而不是内联的。

.style can only reach inline styles, for example: .style 只能达到内联样式,例如:

<div id="hiddenMenuUL" style="display: none;"></div>

Adding 'display: none' inline like in the example above should be the fastest and easiest way to 'fix' it in your particular scenario, although it probably wouldn't be considered the best practice.添加 'display: none' 像上面的例子一样内联应该是在您的特定场景中“修复”它的最快和最简单的方法,尽管它可能不会被认为是最佳实践。

暂无
暂无

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

相关问题 单击一个具有href属性的元素,然后在javascript中更改另一个元素样式,怎么做? - Clicked on an element which has href attribute, then change another elements style in javascript, how to do that? 如何更改element.style的样式? Java脚本 - How do I change an element.style's style? Javascript 如何找到哪个 JavaScript 正在改变元素的样式? - How do I find which JavaScript is changing an element's style? 如何更改数组元素的样式取决于 function 导致 JavaScript? - How to change style of an array element depends on function result in JavaScript? 如何更改“ <input> 动态使用Javascript元素? - How do I change the style of an “<input>” element dynamically using Javascript? 如何更改内部元素的样式<div />用 JavaScript? - How do I change the style of an element inside a <div /> with JavaScript? 使用JavaScript,如何根据另一个元素的CSS样式更改一个元素的CSS样式? - Using JavaScript, how do I change the CSS style of one element based on the CSS style of another element? 如何使用 JavaScript 访问和更改元素的样式? - How to get access and change style of an element with JavaScript? 如何使用 JavaScript 或 JQuery 在页面加载时更改 Div 元素的样式宽度? - How do I change Style Width of Div Element on Page load using JavaScript or JQuery? 更改样式的唯一方法是在JavaScript中按元素进行更改吗? - Is the only way to change a style to do it per-element in JavaScript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM