이벤트의 거품 포획 저지 기본 에이전트
사건의 인식
JS Bin
var btn= document.querySelector('#btn')
btn.onclick = function(e){
console.log(e)
} //e , , 。
// —— MouseEvent {isTrusted: true, screenX: 594, screenY: 87, clientX: 37, clientY: 21…}
altKey: false // alt
bubbles: true //
button: 0
buttons: 0
cancelBubble: false
cancelable: true
clientX: 37 //
clientY: 21 //
composed: true
ctrlKey: false
currentTarget: null
defaultPrevented: false
detail: 1
eventPhase: 0
fromElement: null
isTrusted: true
layerX: 37
layerY: 21
metaKey: false
movementX: 0
movementY: 0
offsetX: 28 //
offsetY: 9 //
pageX: 37
pageY: 21
path: Array(5)relatedTarget: null
returnValue: true
screenX: 594
screenY: 87
shiftKey: false
sourceCapabilities: InputDeviceCapabilities
srcElement: button#btn
target: button#btn // e.target , this btn, 。
timeStamp: 14919.125000000002
toElement: button#btn
type: "click"
view: Window
which: 1
x: 37
y: 21
__proto__: MouseEvent
remove Event Listener가 이벤트를 풀었는데 그 중 두 번째 인자는 함수가 익명 함수일 수 없다는 것이다. 그렇지 않으면 묶을 수 없는add Event Listener와 같다.
IE 호환성
IE 지원:
JS Bin
var btn= document.querySelector('#btn')
btn.onclick = function(e){
console.log(e)
} //e , , 。
// —— MouseEvent {isTrusted: true, screenX: 594, screenY: 87, clientX: 37, clientY: 21…}
altKey: false // alt
bubbles: true //
button: 0
buttons: 0
cancelBubble: false
cancelable: true
clientX: 37 //
clientY: 21 //
composed: true
ctrlKey: false
currentTarget: null
defaultPrevented: false
detail: 1
eventPhase: 0
fromElement: null
isTrusted: true
layerX: 37
layerY: 21
metaKey: false
movementX: 0
movementY: 0
offsetX: 28 //
offsetY: 9 //
pageX: 37
pageY: 21
path: Array(5)relatedTarget: null
returnValue: true
screenX: 594
screenY: 87
shiftKey: false
sourceCapabilities: InputDeviceCapabilities
srcElement: button#btn
target: button#btn // e.target , this btn, 。
timeStamp: 14919.125000000002
toElement: button#btn
type: "click"
view: Window
which: 1
x: 37
y: 21
__proto__: MouseEvent
IE 지원:
이벤트 객체
처음의 예는 이미 사건의 대상을 얻었다. 여기서 비교적 중요한 몇 가지를 꺼내서 말한다.
JS Bin
container
box
target
var btn= document.querySelector('#btn')
btn.onclick = function(e){
console.log(e)
}
document.querySelector('.container').addEventListener('click',function(e){
console.log('container click..in ')
},true)
document.querySelector('.box').addEventListener('click',function(e){
console.log('box click..in ')
},true)
document.querySelector('.target').addEventListener('click',function(e){
console.log('target click..in ')
},true)
document.querySelector('.container').addEventListener('click',function(e){
console.log('container click..in ')
},false)
document.querySelector('.box').addEventListener('click',function(e){
// e.stopPropagation()
console.log('box click..in ')
},false)
document.querySelector('.target').addEventListener('click',function(e){
console.log('target click..in ')
},false)
//"container click..in "
"box click..in "
"target click..in "
"target click..in "
"box click..in "
"container click..in "
// box stopPropagation() ,,,
"container click..in "
"box click..in "
"target click..in "
"target click..in "
"box click..in "
stopPropagation() , , 。
JS Bin
baidu
document.querySelector('a').onclick =
function(e){
e.preventDefault() //
console.log(this.href)
if(/baidu.com/.test(this.href)){ // baidu.com
location.href = this.href //
}
}
오래된 IE의 이벤트 가져오기
윈도우로.이벤트 수령, 진기한 꽃입니다.
흔한 사건
이벤트 에이전트
JS Bin
box1
box2
function $(selector){
return document.querySelector(selector)
}
function $$(selector){
return document.querySelectorAll(selector)
}
$('.box').onclick = function(){
console.log(this.innerText)
}
//$$('.box').onclick = function(){
// console.log(this.innerText)
//} //
// , box1,box2 。
$$, , ?
:
$$('.box')
(2) [div.box, div.box] // , NodeList
$$('.box').onclick
undefined // onclick ! 。
그림과 같다
여기에는ddEvent Listener와onclick이 없지만forEach가 있습니다.$$('.box').forEach(function(node){
node.onclick=function(){
console.log(node.innerText)
}
})
// , , 。
비교적 번거로운데, 부원소에 귀속할 수 있습니까?$('.container').onclick=function(e){
if(e.target.classList.contains('box')){
console.log(e.target.innerText)
}
} //e , , , , , 。
한 걸음 더 깊게: forEach가 꼭 만능은 아니지만,
JS Bin
box1
box2
function $(selector){
return document.querySelector(selector)
}
function $$(selector){
return document.querySelectorAll(selector)
}
$$('.box').onclick = function(){
console.log(this.innerText)
}
$$('.box').forEach(function(node){
node.onclick=function(){
console.log(node.innerText)
}
})
var i =3
$('#add').onclick=function(){
var box =document.createElement('div')
box.classList.add('box')
box.innerText = 'box' +(i++)
$('.container').appendChild(box)
}
// add bix3,box4,box5,,,,,,,
innerText box1,box2, 。
forEach , , 。
프록시 모드에서는 다음을 수행합니다.
JS Bin
box1
box2
function $(selector){
return document.querySelector(selector)
}
function $$(selector){
return document.querySelectorAll(selector)
}
$$('.box').onclick = function(){
console.log(this.innerText)
}
$('.container').onclick=function(e){
if(e.target.classList.contains('box')){
console.log(e.target.innerText)
}
} //
var i =3
$('#add').onclick=function(){
var box =document.createElement('div')
box.classList.add('box')
box.innerText = 'box' +(i++)
$('.container').appendChild(box)
}
, 。 , ,
이벤트 에이전트가 좋아요. 이벤트를 다시 연결할 필요가 없어요. 특히 불러오기를 게을리하거나 하위 요소가 변할 수 있어요.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSON
JSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다.
그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다.
저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.
JS Bin
box1
box2
function $(selector){
return document.querySelector(selector)
}
function $$(selector){
return document.querySelectorAll(selector)
}
$('.box').onclick = function(){
console.log(this.innerText)
}
//$$('.box').onclick = function(){
// console.log(this.innerText)
//} //
// , box1,box2 。
$$, , ?
:
$$('.box')
(2) [div.box, div.box] // , NodeList
$$('.box').onclick
undefined // onclick ! 。
$$('.box').forEach(function(node){
node.onclick=function(){
console.log(node.innerText)
}
})
// , , 。
$('.container').onclick=function(e){
if(e.target.classList.contains('box')){
console.log(e.target.innerText)
}
} //e , , , , , 。
JS Bin
box1
box2
function $(selector){
return document.querySelector(selector)
}
function $$(selector){
return document.querySelectorAll(selector)
}
$$('.box').onclick = function(){
console.log(this.innerText)
}
$$('.box').forEach(function(node){
node.onclick=function(){
console.log(node.innerText)
}
})
var i =3
$('#add').onclick=function(){
var box =document.createElement('div')
box.classList.add('box')
box.innerText = 'box' +(i++)
$('.container').appendChild(box)
}
// add bix3,box4,box5,,,,,,,
innerText box1,box2, 。
forEach , , 。
JS Bin
box1
box2
function $(selector){
return document.querySelector(selector)
}
function $$(selector){
return document.querySelectorAll(selector)
}
$$('.box').onclick = function(){
console.log(this.innerText)
}
$('.container').onclick=function(e){
if(e.target.classList.contains('box')){
console.log(e.target.innerText)
}
} //
var i =3
$('#add').onclick=function(){
var box =document.createElement('div')
box.classList.add('box')
box.innerText = 'box' +(i++)
$('.container').appendChild(box)
}
, 。 , ,
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.