简体   繁体   English

为什么必须重新加载页面才能使JS事件正常工作?

[英]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.

相关问题 如果我不重新加载页面,为什么我的 js 无法正常工作。 这是用于 CS50 Web 项目 3 - Why does my js work incorrectly if i do not reload the page. This is for CS50 Web Project 3 我真的必须重新加载我的页面两次吗? - Do I really have to reload my page twice? 为什么必须重新加载页面才能正确显示地图? - Why do I have to reload the page to display the map correctly? 为什么我必须双击我的按钮才能使活动正常进行? - Why do I have to double-click on my button to make the event work? 为什么我必须单击两次才能在反应 js 中发生事件 - Why do I have to click twice for an event in react js 为什么我有重新加载页面以便在运行服务器时显示脚本? (Gulp / Webpack / React) - Why do I have reload page in order for script to display when I run the server? (Gulp/Webpack/React) 为什么即使有event.preventDefault(表单处理jquery + node.js),我的页面仍要重新加载 - Why is my page reloading even though I have event.preventDefault (form handling jquery + node.js) 我的 if 语句不起作用,为什么单选按钮在我重新加载页面后仍然选中? - my if statement not work, and why radio button still checked after i reload the page? 为什么在用ajax重新加载部分页面后,此javascript代码不起作用? - why this javascript code don't work after i reload part of my page with ajax? 为什么window.onload仅按预期在第一个数组值上起作用,而我必须重新加载页面以获取其他数组值? - Why does window.onload only work as expected on the first array value and I have to reload the page for additional array values?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM