JS丨基础考察03丨DOM事件类
1867 ワード
01.DOM事件類大綱
01. : DOM
DOM0: element.onclick = function(){}
DOM1: DOM1 DOM
DOM2: element.addEventListener('click', function(){}, false)
DOM3: element.addEventListener('keyup', function(){}, false)
02. DOM ; 03. DOM
:
: --> -->
04. DOM
window --> document --> html --> body --> ... -->
05. event
01. event.preventDefault():
02. event.stopPropagation:
03. event.stopImmediatePropagation:
:
:
04. event.currentTarget:
/
05. event.target:
06.
var eve = new Event('custome');
// ev: DOM
ev.addEventListener(
'custome',
function(){
console.log('custome')
}
);
ev.dispatchEvent(eve)
02.盗聴事件の捕獲と発泡
window.onload = function() {
var ev = document.getElementById('ev');
// DOM
ev.addEventListener(
'click',
function(){
console.log('ev captrue')
},
true
);
window.addEventListener(
'click',
function(){
console.log('window captrue')
},
true
);
document.addEventListener(
'click',
function(){
console.log('document captrue')
},
true
);
document.documentElement.addEventListener(
'click',
function(){
console.log('docEle captrue')
},
true
);
document.body.addEventListener(
'click',
function(){
console.log('body captrue')
},
true
);
//
var eve = new Event('newEv');
ev.addEventListener(
'newEv',
function(){
console.log('newEv')
}
)
setTimeout(
function(){
ev.dispatchEvent(eve)//
},
1000)
}