jQuery观察者模式的扩展方法

    xiaoxiao2025-06-18  6

    <script src="http://libs.baidu.com/jquery/1.9.1/jquery.min.js"></script>

    <script type="text/javascript">

    $(function () { $.getJSON('data.json', function (results) { $.publish('app.myevent', results); }); $.subscribe('app.myevent', function(e, results) { console.log(results); }); }); (function($) { var o = $({});//自定义事件对象 $.each({ trigger: 'publish', on: 'subscribe', off: 'unsubscribe' }, function(key, val) { jQuery[val] = function() { o[key].apply(o, arguments); }; }); })(jQuery); $(function () { $.getJSON('data.json', function (results) { $.publish('app.myevent', results); }); $.subscribe('app.myevent', function(e, results) { $('body').html( results.one ); }); }); </script>
    转载请注明原文地址: https://ju.6miu.com/read-1300092.html
    最新回复(0)