简体   繁体   English

我的关闭导航功能在点击时不起作用

[英]my close nav function doesnt work onclick

I'm trying to make left= 0vw on click of menu icon but it doesn't work .我试图在单击菜单图标时使 left= 0vw 但它不起作用。 CSS and html work pretty well I'm a newbie don't make fun if it's so obvious :'( CSS 和 html 工作得很好,我是一个新手,如果它如此明显,请不要取笑:'(

var menuEl = document.getElementsByClassName("bx-menu");
var navEl = document.getElementsByClassName("nav");
function closenav(){
    if(navEl.style.left === "-100vw"){
        navEl.style.left = "0vw";
    } else {
        navEl.style.left = "-100vw";
    }
}

menuEl.addEventListener("click",closenav());

You are using getElementsByClassName() method - it gets element by its class attribute and index.您正在使用getElementsByClassName()方法 - 它通过其类属性和索引获取元素。

Specify what element's index you want to target.指定要定位的元素索引。

var menuEl = document.getElementsByClassName("bx-menu")[0];
var navEl = document.getElementsByClassName("nav")][0];

think he cant read the value of -100vw in line 4认为他无法读取第 4 行中 -100vw 的值

so you have to read out the css value with this:所以你必须用这个读出css值:

const element = document.querySelector('.element')
const style = getComputedStyle(element)

document.getElementsByClassName returns an array of all the elements that have the given class name. document.getElementsByClassName 返回具有给定类名的所有元素的数组。

To get the first element which is probably the one that you want you need to choose the first element of this array.要获得可能是您想要的第一个元素,您需要选择此数组的第一个元素。

Note also the passing of a function in the below.还要注意下面的函数传递。

Here is your altered code这是您更改的代码

var menuEl = document.getElementsByClassName("bx-menu")[0];
var navEl = document.getElementsByClassName("nav")[0];
function closenav(){
    if(navEl.style.left === "-100vw"){
        navEl.style.left = "0vw";
    } else {
        navEl.style.left = "-100vw";
    }
}

menuEl.addEventListener("click",closenav);// Note you need to pass the function here not call the function so I’ve removed the ()

var menuEl = document.getElementsByClassName("bx-menu");
var navEl = document.getElementsByClassName("nav");
function closenav(){
    if(navEl.style.left === "-100vw"){
        navEl.style.left = "0vw";
    } else {
        navEl.style.left = "-100vw";
    }
}

// do you need this twice?? // 你需要两次吗?? If so again remove the () menuEl.addEventListener("click",closenav());如果是这样再次删除 () menuEl.addEventListener("click",closenav());

If the nav bar is unique you may like to look at giving it a unique id and then you can select it using document.getElementById如果导航栏是唯一的,您可能想查看给它一个唯一的 id,然后您可以使用 document.getElementById 选择它

Look specifically at document.getElementsByClassName("bx-menu")[0] , I've added [0] as the getElementsByClassName returns an array of occurences of the class.特别查看document.getElementsByClassName("bx-menu")[0] ,我添加了[0]作为getElementsByClassName返回类出现的数组。

Also be sure to remove the brackets of closenav when adding it as an eventlistener like so: menuEl.addEventListener("click", closenav);在将其添加为closenav时,请务必删除closenav的括号, closenavmenuEl.addEventListener("click", closenav); or encapsulate the whole function inside the eventlistener like I did in the snippet below.或者像我在下面的代码片段中所做的那样,将整个函数封装在事件侦听器中。

 var menuEl = document.getElementsByClassName("bx-menu")[0]; menuEl.addEventListener("click", function (event) { var navEl = document.getElementsByClassName("nav")[0]; if(navEl.style.left === "-100vw"){ navEl.style.left = "0vw"; } else { navEl.style.left = "-100vw"; } });
 .nav { background-color: blue; height: 100px; width: 100px; position: absolute; }
 <div class="bx-menu"> Click here! </div> <div class="nav" style="left: -100vw;"> </div>

An advice is to use #id instead of .class to get the unique elements by using getElementById .一个建议是使用#id而不是.class通过使用getElementById来获取唯一元素。 Otherwise you will end up with an array of elements, if you're using getElementsByClass .否则,如果您使用的是getElementsByClass ,您最终会得到一个元素数组。

I would also suggest to:我还建议:
a) use transform: translate instead of animating with left because left causes recalculation all elements, while transform only takes the moving element into consideration, and a) 使用transform: translate而不是使用left动画,因为 left 会导致重新计算所有元素,而 transform 只考虑移动元素,并且

b) toggle a class with navEl.classList.toggle() instead of looking up style properties in your method. b) 使用navEl.classList.toggle() ) 切换类,而不是在您的方法中查找样式属性。

Don't mind the CSS.不要介意CSS。 It's mostly to set things up.这主要是为了设置东西。

 let navEl = document.getElementById("nav"); let hamburgerEl = document.getElementById("hamburger"); hamburgerEl.addEventListener("click", toggleMenu); function toggleMenu() { navEl.classList.toggle("open"); }
 #nav { position: fixed; left: 0px; top: 0px; width: 200px; height: 400px; padding: 1rem; /* 'rem' is the size of the font in the body element */ text-align: center; background-color: lightblue; border-right: 1px solid darkgrey; transform: translateX(-100%); transition: transform 400ms; } #nav.open { /* the event listener toggles this class */ transform: translateX(0px); } #hamburger { position: relative; /* in order to use z-index to place #hamburger over the menu */ z-index: 100; width: 20px; height: 20px; display: flex; /* to center the strokes */ align-items: center; cursor: pointer; } #strokes, #strokes::before, #strokes::after { width: 100%; height: 2px; background-color: black; } #strokes::before, /* pseudo-elements */ #strokes::after { content: ''; display: block; } #strokes::before { transform: translateY(-6px); } #strokes::after { transform: translateY(4px); }
 <body> <div id="nav"> MENU </div> <div id="hamburger"> <div id="strokes"></div> </div> </body>

为什么onclick function在里面不起作用<div>苗条的标签?</div><div id="text_translate"><p> 我正在开发 VS 代码扩展。 为此,我使用 WebView 来显示用户界面并在其中执行一些任务。 所以我使用 svelte 来自定义我的 WebView 面板。</p><p> 我的问题是,我创建了包含 Styles、JS、HTML 的 svelte 文件。 因此,当我在标签内调用 onclick 方法时,它不起作用,但当它被放置在标签之外时,它就起作用了。 为什么会这样? 我该如何解决这个问题?</p><pre> <h1 style="text-align:center" on:click={microphoneOnAndOff}>Click Me</h1> **--// This work** <div class="box"> <div class="object"> <div class="outline" style="display: none;"></div> <div class="outline" id="delayed" style="display: none;"></div> <div class="buttonStyle"></div> <div class="buttonStyle" id="circlein"> <svg on:click={microphoneOnAndOff} class=""></svg> **--// This Not work** </div> </div> </div></pre><p> 谁能帮我解决这个问题?</p></div> - Why onclick function doesnt work inside <div> tag in svelte?

暂无
暂无

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

相关问题 <a>标记中的</a> jQuery onclick函数<a>第二次不起作用</a> - Jquery onclick function in <a> tag doesnt work on the secondtime Onclick功能无法运行两次以上 - Onclick function doesnt work more than twice img onClick不起作用 - img onClick doesnt work JavaScript onclick()不起作用 - JavaScript onclick() doesnt work 单击一次后,onclick 功能不起作用 - onclick function doesnt work after you click it once 为什么onclick function在里面不起作用<div>苗条的标签?</div><div id="text_translate"><p> 我正在开发 VS 代码扩展。 为此,我使用 WebView 来显示用户界面并在其中执行一些任务。 所以我使用 svelte 来自定义我的 WebView 面板。</p><p> 我的问题是,我创建了包含 Styles、JS、HTML 的 svelte 文件。 因此,当我在标签内调用 onclick 方法时,它不起作用,但当它被放置在标签之外时,它就起作用了。 为什么会这样? 我该如何解决这个问题?</p><pre> <h1 style="text-align:center" on:click={microphoneOnAndOff}>Click Me</h1> **--// This work** <div class="box"> <div class="object"> <div class="outline" style="display: none;"></div> <div class="outline" id="delayed" style="display: none;"></div> <div class="buttonStyle"></div> <div class="buttonStyle" id="circlein"> <svg on:click={microphoneOnAndOff} class=""></svg> **--// This Not work** </div> </div> </div></pre><p> 谁能帮我解决这个问题?</p></div> - Why onclick function doesnt work inside <div> tag in svelte? HTML链接onclick,jquery函数,redirectToAction IIS服务器不起作用? - Html link onclick, jquery function, redirectToAction IIS server doesnt work? 为什么我的onclick不会覆盖先前执行的js函数? - why doesnt my onclick override an earlier executed js function? 为什么有时我的加载功能无法正常工作 - Why sometimes my load function doesnt work 我的添加千位逗号功能不起作用 - My add thousands comma function doesnt work
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM