首页 > 代码库 > jquery事件的发布订阅

jquery事件的发布订阅

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">

</head>
<script
 src="http://code.jquery.com/jquery-latest.js"></script>
<body>
<script type="text/javascript"> 

(function($) {
      var o = $({});
      $.subscribe = function() {
          o.on.apply(o, arguments);
      };
      $.unsubscribe = function() {
          o.off.apply(o, arguments);
      };
      $.publish = function() {
          o.trigger.apply(o, $.makeArray(arguments));
      };
})(jQuery);

//订阅
$.subscribe("test", function(e, a1, a2, a3, a4) {
  console.log(e.type + "," + a1 + "," + a2 + "," + a3 + "," + a4);
});

//发布
$.publish("test", ["a", "b", "c", "d"]);

</script>


</script>
</html>

 技术分享

jquery事件的发布订阅