Code Monkey home page Code Monkey logo

Comments (2)

XinChou16 avatar XinChou16 commented on May 20, 2024

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.

XinChou16 avatar XinChou16 commented on May 20, 2024

发布,订阅模式 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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.