JS-Web-API(二):事件

事件绑定

1
2
3
4
const btn = document.getElementById('btn1')
btn.addEventListener('click', event => {
console.log('clicked')
})
1
2
3
4
5
6
7
8
9
10
// 通用的绑定函数
function bindEvent(elem, type, fn) {
elem.addEventListener(type, fn)
}

const a = document.getElementById('link1')
bindEvent(a, 'click', e => {
e.preventDefault() // 阻止默认行为
alert('clicked')
})
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×