Definition of event monitoring
In Javascript, browsers are generally divided into two categories:
① Browsers based on IE kernel (IE browser with version number less than 9 browser)
② Browser based on W3C kernel (IE browser, Firefox, Google and other browsers with version number greater than 9)
1) Basic syntax: Browser based on IE kernel
dom object.attachEvent(type, callback, capture): Bind an event listener to the element
Parameter description:
type: The bound event type, such as onclick, onmouseover , onmouseout
callback: event handler, usually an anonymous function
capture: the browser model used, bubbling model and capture model. By default, browsers below IE8 only support bubbling. Bubble model!
##1234 56789101112 |
## //封装$函数,用于获取id的元素 function $(id){ return document.getElementById(id) } //绑定事件监听 $('btn').attachEvent('onclick',function(){ alert('hello') }); //二次绑定 $('btn').attachEvent('onclick',function(){ alert('world') }); Copy after login |
Basic syntax: event monitoring based on W3C kernel
2345678910 1112 | //封装$函数,用于获取id的元素 function $(id){ return document.getElementById(id) } //绑定事件监听 $('btn').addEventListener('onclick',function(){ alert('hello') }); //二次绑定 $('btn').addEventListener('onclick',function(){ alert('world') }); Copy after login |
The above is the detailed content of Detailed explanation of js event monitoring examples. For more information, please follow other related articles on the PHP Chinese website!