这里整理的是jquery下js的一些代码组织方法,大家可以借鉴下整理出基于jquery的自己喜欢的模式。 从神奇的"$"函数开始 <a href="javascript:;" id="sayHello">Say Hello</a> <script type="text/javascript"> //when dom ready, do something. //bind click event to a button. $(function(){ $('#sayHello').click(function(){ alert('Hello world!'); }); }); </script>
<a href="javascript:;" id="sayUnlike">Unlike it</a> <script type="text/javascript"> //when dom ready, do something. //bind click event to a button. $(function(){ $('#sayUnlike').click(function(){ alert('I unlike it.'); }); }); </script>
<a href="javascript:;" class="sayUnlike">Unlike it</a> <script type="text/javascript"> //Change to a class selector to match all the button elements. $(function(){ $('.sayUnlike').click(function(){ alert('I unlike it.'); }); }); </script>
<a href="javascript:;" class='sayHello'>Say Hello</a> <a href="javascript:;" class="sayUnlike">Unlike it</a> <script type="text/javascript"> $(function(){ $('.sayHello').click(function(){ alert('Hello world!'); }); $('.sayUnlike').click(function(){ alert('I unlike it.'); }); }); </script>
<? if($page == 'A'){?> <script type="text/javascript"> $(function(){ $('.sayHello').click(function(){ alert('Hello world!'); }); }); </script> <? } ?> <? if($page == 'B'){?> <script type="text/javascript"> $(function(){ $('.sayUnlike').click(function(){ alert('I unlike it.'); }); }); </script> <? } ?>
<? if($page == 'A' or $page == "C" and $page is not "D"){ ?> <script type="text/javascript"> ...... </script> <? } ?> <? if($page == "B" or $page == "E" and $page is not "X"){ ?> <script type="text/javascript"> ..... </script> <? } ?> <? if($page == "B" or $page == "E" or $page == "C"){ ?> <script type="text/javascript"> ..... </script> <? } ?>
<script type="text/javascript"> //Register global name space. var Yottaa = Yottaa || {}; Yottaa.EventMonitor = function(){ this.listeners = {}; } //Bind all event. Yottaa.EventMonitor.prototype.subscribe=function(msg, callback){ var lst = this.listeners[msg]; if (lst) { lst.push(callback); } else { this.listeners[msg] = [callback]; } } // Create the event monitor instance. var event_monitor = new Yottaa.EventMonitor(); function load_event_monitor(root){ var re = /a_(\w+)/; //using a regular expression to filter all event object. var fns = {}; $(".j", root).each(function(i) { var m = re.exec(this.className); if (m) { var f = fns[m[1]]; if (!f) { //如果事件处理函数不存在则创建函数对象. f = eval("Yottaa.init_"+m[1]); fns[m[1]] = f;//调用绑定函数. } f && f(this); } }); } $(function(){ // when dom ready, bind all event. load_event_monitor(document); }); //Here is 2 sample components. Yottaa.init_sayhello = function(obj){ $(obj).click(function(){ alert('Hello world!'); }); } Yottaa.init_unlike = function(obj){ $(obj).click(function(){ alert('I unlike it.'); }); } </script>
<script type="text/javascript"> Yottaa.init_sampleajax = function(obj){ $(obj).click(function(){ var component_id = $(this).attr('id').split('-')[1]; $.get('/server/controller/method', {id: component_id}, function(data){ if(data){ alert('Message from server: ' + data ); } }); }); } </script> <a href="javascript:;" class='j a_sampleajax' id='item-a'>Show server message. </a> <a href="javascript:;" class='j a_sampleajax' id="item-b">Another button with same action but different server side identifier.</a>
Yottaa.globalConst = { User:{ familyName: "Jhone", givenName: 'bruce' }, Url:{ siteName: 'yottaa.com', score: 98 } } Yottaa.componentMetaData = { compoment_id_1:{ ...... }, component_id_2:{ ...... } };
<script type="text/javascript"> function init_loginPanel = function(){ var container = $('login_panel'); $('#login_button').click(function(){ ...... }); } function init_chart = function(){ ...... } //global static init method Yottaa.initComponents = function(components){ for(var i = 0;i<components.length;i++){ if(typeof window[components[i]] == 'Function'){ window[components[i]](); } } } // above is in the 'all-in-one' assets file which is compressed to one file in production. var components = ['init_loginPanel', 'init_chart']; var metaData = { loginPanel: {}, chart: {}, ...... }; $(function(){ Yottaa.initComponents(components); }); //here is inline script on the page. </script> (责任编辑:admin) |