[英]Why do I have to reload the page for my JS event to work?
Hello I created an on click nav bar for my homepage. 您好,我为我的主页创建了一个点击式导航栏。 I would like that the user clicks and then the nav bar opens.
我希望用户单击,然后打开导航栏。 My code does work, however it only works one or two times and then it doesn't work the third time until I refresh the page.
我的代码可以工作,但是只能工作一到两次,然后在刷新页面之前第三次不能工作。
Does anyone know why this is? 有人知道为什么是这样吗?
var sliderMenu = document.getElementById("slider-menu"); var close = document.getElementById("X"); sliderMenu.addEventListener("click", (event) => { activate(); console.log(event) }); close.addEventListener("click", (event) => { closeNav(); }); function activate(event) { var sliderMenuOpen = document.getElementById("slider-menu-open"); sliderMenuOpen.classList.toggle("width"); } function closeNav(event) { var sliderMenuOpen = document.getElementById("slider-menu-open"); sliderMenuOpen.classList.toggle("width-close"); }
<div class="slider-menu" id="slider-menu"> <p>More</p> </div> <div class="slider-menu-open" id="slider-menu-open"> <a id="X" href="">X</a> <a href="">Carpet Cleaning</a> <a href="">Blinds</a> <a href="">Flooring</a> <a href="">Rugs</a> </div> </div>
Try something like this instead. 尝试这样的事情。 It removes the need for the extra class
width-close
它消除了额外的类
width-close
// i moved this to outside the functions so that you don't have to keep
// unnecessarily setting sliderMenuOpen
var sliderMenuOpen = document.getElementById("slider-menu-open");
var sliderMenu = document.getElementById("slider-menu");
var close = document.getElementById("X");
sliderMenu.addEventListener("click", (ev)=>{
sliderMenuOpen.classList.add("width");
});
close.addEventListener("click", (ev)=>{
sliderMenuOpen.classList.remove("width");
});
If you check the DOM with your old code you'll notice that they'll both be active (or inactive) at the wrong times after clicking close and open a few times. 如果用旧代码检查DOM,您会发现在单击关闭并打开几次后,它们将在错误的时间处于活动状态(或处于非活动状态)。
If you could share your CSS that would be helpful. 如果您可以共享CSS,那将会有所帮助。
Whilst I'm not sure what your CSS is doing, the issue could be that you are toggling width
when clicking More
and width-close
when clicking X
. 虽然我不确定您的CSS在做什么,但问题可能是您单击“
More
时切换了width
,而单击X
时切换了width
width-close
。
On clicking More
you add width
class, then on clicking close you add width-close
then when you click open again it REMOVES width
(instead of adding it which is what it did the first time). 单击
More
,添加width
类,然后单击关闭,添加width-close
然后再次单击打开时,它删除width
(而不是第一次添加width
)。 then you click close and it adds width-close
again on its own. 然后单击关闭,它会再次自行添加
width-close
。
this could be the cause, but without seeing the CSS I'm not sure. 这可能是原因,但是我不确定我没有看到CSS。
It could be better if there was just one toggle function for both the close and the open which the X and the More would both use. 如果X和More都将同时使用关闭和打开的一个切换功能,那会更好。 You could then update the
More
text to say Less
when the menu is open 打开菜单后,您可以将
More
文本更新为Less
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.