Home > Web Front-end > JS Tutorial > Detailed explanation of JavaScript event binding, triggering and deletion sample code

Detailed explanation of JavaScript event binding, triggering and deletion sample code

伊谢尔伦
Release: 2017-07-22 17:20:15
Original
1692 people have browsed it

JavaScript The simplest event model requires event binding and triggering, as well as event deletion.


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] === &#39;function&#39;) {
   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 === &#39;string&#39;) {
  if (handlers.length === 0) {
  this.list[type] = [];
  } else {
  for (var i = 0; i < handlers.length; i++) {
   if (typeof handlers[i] === &#39;function&#39; && 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));
 }
 }
};
Copy after login

It mainly implements bind (binding event), unbind (deletion event) and trigger (trigger event). For the same event name, multiple event processing functions can be bound; they will be triggered sequentially in the order of binding.

args.splice(0, handlers[i].length) Parameters passed when triggered

Event binding and triggering:


eventModel.bind(&#39;myevent1&#39;, function (a) {
 console.log(a); // 1
}, function(b) {
 console.log(b); // 2
}, function(c, d) {
 console.log(c + &#39; + &#39; + d); // a + b
});
eventModel.bind(&#39;myevent1&#39;, function (e) {
 console.log(e); // 50
});
eventModel.trigger(&#39;myevent1&#39;, 1,2,&#39;a&#39;,&#39;b&#39;, 50);
Copy after login

Event deletion:


<button id="bind">bind</button>
<button id="unbind">unbind</button>
Copy after login


var fnX = function () {
 console.log(&#39;fnX&#39;);
}
var fnY = function () {
 console.log(&#39;fnY&#39;);
}
eventModel.bind(&#39;myevent2&#39;, fnX, fnY);
document.getElementById(&#39;unbind&#39;).onclick = function () {
 eventModel.unbind(&#39;myevent2&#39;, fnX); //删除 fnX 后,只剩下 fnY
};
document.getElementById(&#39;bind&#39;).onclick = function () {
 eventModel.trigger(&#39;myevent2&#39;); //输出 fnX fnY
 //在点击unbind后,只输出 fnY
};
Copy after login

The above is the detailed content of Detailed explanation of JavaScript event binding, triggering and deletion sample code. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template