简体   繁体   English

addEventListener 在第一次点击时不起作用

[英]addEventListener not working on first click

This code is for opening and closing an accordion menu, but the event I added to 'acc' does not work on the first click.此代码用于打开和关闭手风琴菜单,但我添加到“acc”的事件在第一次点击时不起作用。

 let acc = document.getElementById("acc"); let list = document.getElementById("list"); let rest = document.getElementById("rest"); let nav = document.getElementById("nav"); let line = document.getElementsByClassName("line") acc.addEventListener("click", () => { if (list.style.left == '-50%') { list.style.left = "0"; Array.prototype.forEach.call(line, function(value, index) { line[index].style.backgroundColor = "lightBlue"; }); } else { list.style.left = "-50%"; Array.prototype.forEach.call(line, function(value, index) { line[index].style.backgroundColor = 'rgba(0, 0, 0, 0.781)'; }); } }); rest.addEventListener("click", () => { list.style.left = "-50%"; Array.prototype.forEach.call(line, function(value, index) { line[index].style.backgroundColor = 'rgba(0, 0, 0, 0.781)'; }); })
 <nav class="nav" id="nav"> <p class="header">koooooooooooooon</p> <button class="accordion" id="acc"> <div class="wrap"> <div class="line"></div> <div class="line"></div> <div class="line"></div> </div> </button> </nav> <div class="list" id="list"> <ul class="ul"> <li><a href="#">home</a></li> <li><a href="konkor.html">konkor</a></li> <li><a href="arshad.html">konkor arshad</a></li> <li><a href="nahaii.html">emtehan nahaii</a></li> </ul> </div> <div class="rest" id="rest"> <p class="p">welcome to our lil app</p> </div>

I couldn't think of any other way to do this.我想不出任何其他方法来做到这一点。

For this syndrome the tipical problem is: CSS overrides.对于这种综合症,典型的问题是:CSS 覆盖。 If you hide by default via css class your menu, then in the first click the script will detect the oposit what you think it will.如果默认情况下通过 css 类隐藏菜单,则在第一次单击时,脚本将检测到您认为会发生的情况。 Set the initial value in the style attribute, or use classes to determinate the current state of the elements.在 style 属性中设置初始值,或者使用类来确定元素的当前状态。 A little explanation: if you use only pure JavaScript, it won't take care about the style settings which you set on the Element with classes.一点解释:如果你只使用纯 JavaScript,它不会关心你在带有类的Element上设置的样式设置。 It only returns those style which are directly set on the element with the style attribute.它只返回那些直接在具有style属性的元素上设置的style

ie. IE。 You set via css the Left property of the element to -50%.您通过 css 将元素的 Left 属性设置为 -50%。 The JavaScript will return "" if you call the style.left because it can't detect the CSS class' value.如果您调用style.left ,JavaScript 将返回"" ,因为它无法检测 CSS 类的值。 Then on second click it will work properly and it will return -50% than 0 than again -50% .然后在第二次点击它会正常工作,它会返回-50%0再次-50% This will be invisible because, the style attribute will be set to a value on first click which is the default value you set with a class previously.这将是不可见的,因为样式属性将在第一次单击时设置为一个值,这是您之前为类设置的默认值。

Also I want to note, this is not true in case of jQuery.另外我想指出,在 jQuery 的情况下,情况并非如此。 If you using jQuery it detects somehow the attributes on the Elements , which was applied via classes.如果您使用 jQuery,它会以某种方式检测Elements上的Elements ,这些属性是通过类应用的。

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

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