最簡單的辦法,就只綁定一個事件不就好了;app
第二種,我以爲和第一種也沒啥區別。。spa
const Button = document.getElementById("targetButton"); const clickEvent = (function() { if ('ontouchstart' in document.documentElement === true) return 'touchstart'; else return 'click'; })(); Button.addEventListener(clickEvent, e => { console.log("things happened!"); })