欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來到入門教程網(wǎng)!

C#教程

當前位置:主頁 > 軟件編程 > C#教程 >

js事件模型與自定義事件實例解析

來源:本站原創(chuàng)|時間:2020-01-10|欄目:C#教程|點擊: 次

JavaScript 一個最簡單的事件模型,需要有事件綁定與觸發(fā),還有事件刪除。

var eventModel = {
 list: {},
 bind: function () {
 var args = [].slice.call(arguments),
 type = args[0],
 handlers = args.slice(1);
 if (typeof type === 'string' && handlers.length > 0) {
  for (var i = 0; i < handlers.length; i++) {
  if (typeof handlers[i] === 'function') {
   if (!this.list[type]) {
   this.list[type] = [];
   }
   this.list[type].push(handlers[i]);
  }
  }
 }
 },
 unbind: function () {
 var type = arguments[0],
 handlers = Array.prototype.slice.call(arguments, 1);
 if (typeof type === 'string') {
  if (handlers.length === 0) {
  this.list[type] = [];
  } else {
  for (var i = 0; i < handlers.length; i++) {
   if (typeof handlers[i] === 'function' && handlers[i] === this.list[type][i]) {
   this.list[type].splice(i, 1);
   }
  }
  }
 }
 },
 trigger: function () {
 var arguments = [].slice.call(arguments),
 type = arguments[0],
 args = arguments[1] instanceof Array && !arguments[2] ? arguments[1] : arguments.slice(1),
 handlers = this.list[type];
 for (var i = 0; i < handlers.length; i++) {
  handlers[i].apply(this, args.splice(0, handlers[i].length));
 }
 }
};

其中主要實現(xiàn)了bind(綁定事件)、unbind(刪除事件)與 trigger (觸發(fā)事件)。對同一事件名稱,可以綁定多個事件處理函數(shù);并按照綁定的順序依次觸發(fā)。

args.splice(0, handlers[i].length) 觸發(fā)時的傳參

事件綁定與觸發(fā):

eventModel.bind('myevent1', function (a) {
 console.log(a); // 1
}, function(b) {
 console.log(b); // 2
}, function(c, d) {
 console.log(c + ' + ' + d); // a + b
});
eventModel.bind('myevent1', function (e) {
 console.log(e); // 50
});
eventModel.trigger('myevent1', 1,2,'a','b', 50);

事件刪除:

<button id="bind">bind</button>
<button id="unbind">unbind</button>
var fnX = function () {
 console.log('fnX');
}
var fnY = function () {
 console.log('fnY');
}
eventModel.bind('myevent2', fnX, fnY);
document.getElementById('unbind').onclick = function () {
 eventModel.unbind('myevent2', fnX); //刪除 fnX 后,只剩下 fnY
};
document.getElementById('bind').onclick = function () {
 eventModel.trigger('myevent2'); //輸出 fnX fnY
 //在點擊unbind后,只輸出 fnY
};

以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對大家的學習或者工作能帶來一定的幫助,同時也希望多多支持我們!

上一篇:淺談Async和Await如何簡化異步編程(幾個實例讓你徹底明白)

欄    目:C#教程

下一篇:詳解C# TimeSpan 計算時間差(時間間隔)

本文標題:js事件模型與自定義事件實例解析

本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/6061.html

網(wǎng)頁制作CMS教程網(wǎng)絡編程軟件編程腳本語言數(shù)據(jù)庫服務器

如果侵犯了您的權(quán)利,請與我們聯(lián)系,我們將在24小時內(nèi)進行處理、任何非本站因素導致的法律后果,本站均不負任何責任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有