<button id="button">click me then change word</button>
var button = document.getElementById('button'); button.addEventListener('click',function (event) { console.log(event); });
firefox67 | chrome72 | edge44.17763.1.0 | ie11 | ie9 |
---|---|---|---|---|
altKey | altKey | altKey | altKey | altKey |
bubbles | bubbles | bubbles | AT_TARGET | AT_TARGET |
button | button | button | bubbles | bubbles |
buttons | buttons | buttons | BUBBLING_PHASE | BUBBLING_PHASE |
cancelBubble | cancelBubble | cancelable | button | button |
cancelable | cancelable | cancelBubble | buttons | buttons |
clientX | clientX | clientX | cancelable | cancelable |
clientY | clientY | clientY | cancelBubble | cancelBubble |
composed | composed | ctrlKey | CAPTURING_PHASE | CAPTURING_PHASE |
ctrlKey | ctrlKey | currentTarget | clientX | clientX |
currentTarget | currentTarget | defaultPrevented | clientY | clientY |
defaultPrevented | defaultPrevented | detail | constructor | constructor |
detail | detail | eventPhase | ctrlKey | ctrlKey |
eventPhase | eventPhase | fromElement | currentTarget | currentTarget |
explicitOriginalTarget | fromElement | height | defaultPrevented | defaultPrevented |
isTrusted | isTrusted | isPrimary | detail | detail |
layerX | layerX | isTrusted | deviceSessionId | eventPhase |
layerY | layerY | layerX | eventPhase | fromElement |
metaKey | metaKey | layerY | fromElement | isTrusted |
movementX | movementX | metaKey | height | layerX |
movementY | movementY | movementX | hwTimestamp | layerY |
mozInputSource | offsetX | movementY | isPrimary | metaKey |
mozPressure | offsetY | offsetX | isTrusted | offsetX |
offsetX | pageX | offsetY | layerX | offsetY |
offsetY | pageY | pageX | layerY | pageX |
originalTarget | path | pageY | metaKey | pageY |
pageX | relatedTarget | pointerId | offsetX | relatedTarget |
pageY | returnValue | pointerType | offsetY | screenX |
rangeOffset | screenX | pressure | pageX | screenY |
rangeParent | screenY | relatedTarget | pageY | shiftKey |
region | shiftKey | returnValue | pointerId | srcElement |
relatedTarget | sourceCapabilities | screenX | pointerType | target |
returnValue | srcElement | screenY | pressure | timeStamp |
screenX | target | shiftKey | relatedTarget | toElement |
screenY | timeStamp | srcElement | rotation | type |
shiftKey | toElement | target | screenX | view |
srcElement | type | tiltX | screenY | which |
target | view | tiltY | shiftKey | x |
timeStamp | which | timeStamp | srcElement | y |
type | x | toElement | target | |
view | y | twist | tiltX | |
which | type | tiltY | ||
x | view | timeStamp | ||
y | which | toElement | ||
width | type | |||
x | view | |||
y | which | |||
width | ||||
x | ||||
y |
button.addEventListener('click',function (event) { console.log(event.currentTarget === this); // true });
<div id="wrap"> <button id="button">test click</button> </div>
var wrap = document.getElementById('wrap'), button = document.getElementById('button'); // 注意我们监听的是wrap的click事件,而不是button的click事件 wrap.addEventListener('click',function (event) { // event.target指向的是按钮,因为我们点击的是按钮 console.log(event.target === button && event.target === event.srcElement); // true // 当我们点击按钮触发的事件冒泡到了wrap,所以触发了wrap的click事件, // 此时currentTarget指向的是wrap console.log(wrap===this && wrap === event.currentTarget); // true // 直接打印event然后控制台中查看currentTaget会返回null // 你可以将他赋值到一个变量在打印输出这个变量 // see https://github.com/vuejs/vue/issues/6867#issuecomment-338195468 })
// Make a list var ul = document.createElement('ul'); document.body.appendChild(ul); var li1 = document.createElement('li'); var li2 = document.createElement('li'); ul.appendChild(li1); ul.appendChild(li2); function hide(e){ // e.target 引用着 <li> 元素 // 不像 e.currentTarget 引用着其父级的 <ul> 元素. e.target.style.visibility = 'hidden'; } // 添加监听事件到列表,当每个 <li> 被点击的时候都会触发。 ul.addEventListener('click', hide, false);
https://developer.mozilla.org...
Event.srcElement 是标准的 Event.target 属性的一个别名。它只对老版本的IE浏览器有效。
const a = new EventTarget; a.addEventListener('click',()=>{ })
// --- 包装EventTarget开始 function MyEventTarget() { var target = document.createTextNode(null); this.addEventListener = target.addEventListener.bind(target); this.removeEventListener = target.removeEventListener.bind(target); this.dispatchEvent = target.dispatchEvent.bind(target); } MyEventTarget.prototype = EventTarget.prototype; // --- 包装EventTarget结束 // --- 创建我们继承EventTarget的构造函数 function myElem() { } myElem.prototype = new MyEventTarget; myElem.prototype.constructor = myElem; // 创建实例 const instance = new myElem(); instance.addEventListener('click',()=>{ // 现在我们实例可以监听事件了 }); console.log(instance);
详细参考:https://stackoverflow.com/que...
document.getElementById('button').addEventListener('click',(event)=>{ // event console.log(event); })
// 使用全局的Event new Event('test',{ // 事件类型 bubbles:false, // 是否冒泡 默认false cancelable:false,// 是否可以被取消 默认false });
https://developer.mozilla.org...
一篇值得参考的文章,使用createEvent api
function MyEventTarget() { var target = document.createTextNode(null); this.addEventListener = target.addEventListener.bind(target); this.removeEventListener = target.removeEventListener.bind(target); this.dispatchEvent = target.dispatchEvent.bind(target); } MyEventTarget.prototype = EventTarget.prototype; function myElem() { } myElem.prototype = new MyEventTarget; myElem.prototype.constructor = myElem; const instance = new myElem(); instance.addEventListener('test', (event) => { console.log(event); // 监听事件并且打印实例 }); const myEvent = new Event('test'); // 创建Event实例 instance.dispatchEvent(myEvent); // 触发事件
详细参考https://developer.mozilla.org...
<div id="wrap"> <button id="button">test click</button> </div>
const button = document.getElementById('button'), wrap = document.getElementById('wrap'); wrap.addEventListener('click', (event) => { console.log(event); // 打印event对象 }); const myEvent1 = new Event('click', { bubbles: false, // 不可以冒泡 }); const myEvent2 = new Event('click', { bubbles: true, // 可以冒泡 }); button.dispatchEvent(myEvent1); // 这次没有打印出内容 button.dispatchEvent(myEvent2); // 这次打印出了内容
本站采用系统自动发货方式,付款后即出现下载入口,如有疑问请咨询在线客服!
售后时间:早10点 - 晚11:30点Copyright © 2024 jiecseo.com All rights reserved. 粤ICP备18085929号
欢迎光临【捷杰建站】,本站所有资源仅供学习与参考,禁止用于商业用途或从事违法行为!
技术营运:深圳市晟艺互动传媒有限公司