行间事件
<button onclick='functon()'></button>
js中绑定
<button id='btn'></button>
const btn = document.getElementById('btn');
绑定事件:
btn.onclick = function() {}
注销事件:
btn.onclick = null
注:相同事件会覆盖
addEventListener注册
<button id='btn'></button>
const btn = document.getElementById('btn');
btn.addEventListener('click', function() {
})
绑定事件兼容封装:
function addEvent(domNode,eventType,handlerFunction) {
if (window.addEventListener) {
domNode.addEventListener(eventType,handlerFunction);
}else {
domNode.attachEvent(`on${eventType}`,handlerFunction);
}
}
移除事件兼容封装:
function removeEvent(domNode,eventType,handlerFunction) {
if (window.removeEventListener) {
domNode.removeEventListener(eventType,handlerFunction);
}else {
domNode.detachEvent(`on${eventType}`,handlerFunction);
}
}
网友评论