原生js怎么獲取事件對象


1、對某個元素進行事件觸發時,比如點擊事件時,想獲取這個事件對象,這時候可以通過如下方式獲取

<input type="button" value="測試" onclick="test()" />
function test(e){
            const event = e || window.event
            console.log(event, 'event')
        }

 

2、帶參數的事件函數怎么獲取事件對象

<input type="button" value="測試" onclick="test(event,2)" />
function test(e, other){
            const event = e || window.event
            console.log(event, other, 'event')
        }

在傳入你自己的參數前,先把event放在第一個參數傳入(注意參數名event和傳入必須是第一個參數位置),然后就可以在該事件回調函數中獲取事件對象,傳統獲取e||window.event

參考:https://www.cnblogs.com/dcyd/p/12482989.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM