jQuery進行組件開發(fā)完整實例
本文實例講述了jQuery進行組件開發(fā)的方法,分享給大家供大家參考,具體如下:
前面的《JavaScript組件開發(fā)》分析了JavaScript進行組件開發(fā)的技巧,這里分析使用jQuery進行組件開發(fā)的方法。
使用jQuery進行組件開發(fā)和使用純JavaScript腳本(不使用框架)原理基本類似,特別是公共方法的組織是一樣的。
不同點是,jQuery使用了插件機制,通過$()直接進行操作對象(DOM元素)綁定,然后對DOM元素或HTML代碼進行綁定事件等的操作。
另一個不同點則是把jQuery當做工具來使用,用來創(chuàng)建DOM對象,快速查找指定DOM對象等。
例子測試通過。
初級簡單示例,只實現(xiàn)了增加頁和選擇頁功能。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title> Design JS component with jQuery </title> <script src="jquery.js" type="text/javascript"></script> <link href="tabs.css" rel="stylesheet" type="text/css" /> <style> .tabsDiv{width: 500px;height: 350px;margin-top: 0px;margin-left: 0px;} .tabsDiv ul{ width: 500px;height: 20px; list-style: none; margin-bottom: 0px;margin: 0px; padding: 0px; border-left:solid 1px #ffffff;border-right:solid 1px #ffffff;border-top:solid 1px #ffffff;border-bottom:solid 1px #e0e0e0; } .tabsDiv div{ width: 500px;height: 330px; background-color: #ffffff; border:solid 1px #e0e0e0; } .tabsSeletedLi{ width: 100px;height: 20px; background-color: white; float: left; text-align: center; border-left:solid 1px #e0e0e0;border-right:solid 1px #e0e0e0;border-top:solid 1px #e0e0e0;border-bottom:solid 1px #ffffff; } .tabsSeletedLi a{ width: 100px; height: 20px; color:#000000; text-decoration:none; } .tabsUnSeletedLi{ width: 100px;height: 20px; background-color: #e0e0e0; float: left; text-align: center; border:solid 1px #e0e0e0; } .tabsUnSeletedLi a{ width: 100px;height: 20px; color: #ffffff; text-decoration:none; } </style> </head> <body> <!-- <div style="width:400px;height:100px;border:solid 1px #e0e0e0;"> </div> --> <!--tabs示例--> <div id="mytabs"> <!--選項卡區(qū)域--> <ul> <li><a href="#tabs1">選項1</a></li> <li><a href="#tabs2">選項2</a></li> <li><a href="#tabs3">選項3</a></li> </ul> <!--面板區(qū)域--> <div id="tabs1">11111</div> <div id="tabs2">22222</div> <div id="tabs3">33333</div> </div> <script lang="javascript"> (function ($) { $.fn.tabs = function (options) { var me = this; //使用鼠標移動觸發(fā),亦可通過click方式觸發(fā)頁面切換 var defualts = { switchingMode: "mousemove" }; //融合配置項 var opts = $.extend({}, defualts, options); //DOM容器對象,類似MX框架中的$e var $e = $(this); //選中的TAB頁索引 var selectedIndex = 0; //TAB列表 var $lis; //PAGE容器 var aPages = []; //初始化方法 me.init = function(){ //給容器設置樣式類 $e.addClass("tabsDiv"); $lis = $("ul li", $e); //設置TAB頭的選中和非選中樣式 $lis.each(function(i, dom){ if(i==0){ $(this).addClass("tabsSeletedLi") }else{ $(this).addClass("tabsUnSeletedLi"); } }); //$("ul li:first", $e).addClass("tabsSeletedLi"); //$("ul li", $e).not(":first").addClass("tabsUnSeletedLi"); //$("div", $e).not(":first").hide(); //TAB pages綁定 var $pages = $('div', $e); $pages.each(function(i, dom){ if(i == 0){ $(this).show(); }else{ $(this).hide(); } aPages.push($(this)); }); //綁定事件 $lis.bind(opts.switchingMode, function() { var idx = $lis.index($(this)) me.selectPage(idx); }); } /** * 選中TAB頁 * */ me.selectPage = function(idx){ if (selectedIndex != idx) { $lis.eq(selectedIndex).removeClass("tabsSeletedLi").addClass("tabsUnSeletedLi"); $lis.eq(idx).removeClass("tabsUnSeletedLi").addClass("tabsSeletedLi"); aPages[selectedIndex].hide(); aPages[idx].show(); selectedIndex = idx; }; } me.showMsg = function(){ alert('WAHAHA!'); } //自動執(zhí)行初始化函數(shù) me.init(); //返回函數(shù)對象 return this; }; })(jQuery); </script> <script type="text/javascript"> /* $(function () { $("#mytabs").tabs; }); */ var tab1 = $("#mytabs").tabs(); tab1.showMsg(); </script> </body> </html>
最終效果如圖所示:
希望本文所述對大家jQuery程序設計有所幫助。
相關文章
使用jQuery判斷Div是否在可視區(qū)域的方法 判斷div是否可見
這篇文章主要介紹了使用jQuery判斷Div是否在可視區(qū)域的方法 判斷div是否可見2016-02-02一些主流JS框架中DOMReady事件的實現(xiàn)小結
在實際應用中,我們經(jīng)常會遇到這樣的場景,當頁面加載完成后去做一些事情:綁定事件、DOM操作某些結點等。2011-02-02jquery ajax 如何向jsp提交表單數(shù)據(jù)
ajax技術在做表單數(shù)據(jù)傳值非常棒,給用戶帶來很好的用戶體驗度,同時,使用jquery可以簡化開發(fā),提高效率。下面給大家介紹jquery ajax 如何向jsp提交表單數(shù)據(jù),需要的朋友可以參考下2015-08-08淺談JQ中mouseover和mouseenter的區(qū)別
下面小編就為大家?guī)硪黄獪\談JQ中mouseover和mouseenter的區(qū)別。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。2016-09-09jquery中append()與appendto()用法分析
這篇文章主要介紹了jquery中append()與appendto()用法分析,以實例的形式分析了jquery中append()與appendto()的具體語法與詳細用法,需要的朋友可以參考下2014-11-11