Comments (2)
DOM event
// 监听事件, DOM0, DOM2, IE
var addEvent = function(elem, type, handler, useBubble) {
if(elem.addEventListener) {
elem.addEventListener(type, handler, useBubble);
}else if(elem.attachEvent) {
elem.attachEvent('on'+type, function(){
handler.call(elem)
});
}else {
elem['on' + type] = handler;
}
};
// 事件取消注册
var delEvent = function(elem, type, handler, useBubble) {
if(elem.removeEventListener) {
elem.removeEventListener(type, handler, useBubble);
}else if(elem.detachEvent) {
elem.detachEvent('on'+type, handler);
}else {
elem['on' + type] = null;
}
}
// 阻止事件冒泡,IE只有冒泡过程
var stopPropagation = function (elem ) {
if(elem.stopPropagation) {
elem.stopPropagation();
}else {
elem.cancelBubble = true;
}
}
// 取消默认行为
var preventDefault = function(event) {
if(event.preventDefault) {
event.preventDefault();
}else {
event.returnValue = false;
}
}
// 获取事件目标
var getTarget = function(event) {
return event.target || event.srcElement;
}
// 获取事件对象
var getEvent = function(event) {
return event || window.event; // IE下的事件对象实在wind
}
from demos.
发布,订阅模式 ES6
class Event {
constructor() {
this.events = {};
}
// 注册事件
on(type,cb) {
if( this.events.hasOwnProperty(type) ) {
this.events[type].push(cb)
}
this.events[type] = [cb]
}
// 移除事件
off(type) {
if( this.events.hasOwnProperty(type) ) {
delete this.events[type]
}
}
// 触发事件
emit(type, ...args) {
this.events[type].forEach( cb => {
cb(args)
})
}
}
var event = new Event();
event.on('transmit', (args) => {
console.log('成功注册事件 '+args)
})
event.emit('transmit','我是触发事件传入的参数','参数2'); //
event.off('transmit');
event.emit('transmit','1'); //"TypeError: Cannot read property 'forEach' of undefined
from demos.
Related Issues (7)
- JS-数组方法的实现 HOT 5
- JS-对象 HOT 2
- JS-字符串 HOT 1
- JS-运算符 HOT 3
- JS-正则 HOT 1
- JS-Polyfill
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from demos.