2 回答

TA贡献1900条经验 获得超5个赞
使用事件委托,您只需在最外层的父级上设置事件处理程序并在那里处理所有事件。在处理程序中,您测试event.target以查看事件的源元素是什么,并且仅当元素是您想要的元素时才继续。
这是一个click事件示例:
document.querySelector(".parent").addEventListener("click", function(event){
console.log("Either the parent or the child was clicked.");
// Only react if the source of the event was the parent, not the child.
if(event.target.classList.contains("parent")){
alert("you clicked on the parent");
}
});
<div>Click on both the parent and the child. Only the parent will perform the desired action.
<div class="parent">
I'm the parent
<div class="child">
I'm the child
</div>
</div>
</div>

TA贡献1825条经验 获得超6个赞
最后,我能够解决问题的方法是完全删除 onleave 事件。这是导致大多数问题的原因。
所以,现在在 onenter 上,我只是检查 currentTarget 是否与之前访问过的 currentTarget 不同。如果是,则禁用之前访问过的 currentTarget 的指标,并激活新的 currentTarget 的指标。
这实际上比我预期的要好,因为即使您离开卡,但没有输入新卡,指示器也会保持不变。这样您就可以随时跟踪您上次进入的位置。
添加回答
举报