2 回答

TA貢獻1853條經(jīng)驗 獲得超18個贊
使用事件委托,您只需在最外層父級上設(shè)置事件處理程序并處理那里的所有事件。在處理程序中,您可以測試event.target
以查看事件的源元素是什么,并且僅當(dāng)該元素是您想要的元素時才繼續(xù)。
這是一個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貢獻1865條經(jīng)驗 獲得超7個贊
最后,我解決問題的方法是完全刪除 onleave 事件。這是造成大多數(shù)問題的原因。
因此,現(xiàn)在在 onenter 上,我只是檢查 currentTarget 是否與之前訪問過的 currentTarget 不同。如果是,則禁用先前訪問的 currentTarget 的指示器,并激活新的 currentTarget 的指示器。
這實際上比我預(yù)期的要好,因為即使您離開卡但沒有輸入新卡,該指示器也會保持不變。這樣您就可以隨時跟蹤您上次輸入的位置。
- 2 回答
- 0 關(guān)注
- 147 瀏覽
添加回答
舉報