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