DOM classList.remove 仅在单击两次而不是单击一次后才起作用
我正在开发一个应用程序,我想从一个元素中删除 classList 并将 classList 添加到第二个元素。我所发生的情况是,第二个元素在第一次单击时添加了其 classList,但第一个元素直到第二次单击才删除其 classList。
这是事件处理程序:
const resetCount = () => {
setCount(initialCount);
setGrid(initialState);
const active = document.querySelector(".active");
active.classList.remove("active");
const five = document.querySelector(".five");
five.classList.add("active");
}
我通过 onClick 将其连接到我的按钮。
正如我所提到的,当我单击按钮时,const 5 会添加类名“active”,但 const active 不会删除类名“active”。当我第二次单击时,cont active 就会删除类名“active”。
由于这两个事件彼此相邻并且在同一函数中,我很困惑为什么删除(“active”)事件仅在第二次单击后才起作用?任何澄清将不胜感激。
I am working on an app where I want to remove a classList from one element and add a classList to a second. What I have happening though is the second element gets its classList added on the first click, but the first element does not get its classList removed until the second click.
Here is the event handler:
const resetCount = () => {
setCount(initialCount);
setGrid(initialState);
const active = document.querySelector(".active");
active.classList.remove("active");
const five = document.querySelector(".five");
five.classList.add("active");
}
I have this connected to my button via onClick.
As I mentioned, when I click the button the const five gets the class name "active" added, but the const active does not get the class name "active" removed. When I click a second time, the cont active then gets the class name "active" removed.
Since the two event are next to each other and in the same function, I'm confused as to why the remove("active") event only works after the second click? Any clarification would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论