jQuery進(jìn)行組件開(kāi)發(fā)完整實(shí)例
本文實(shí)例講述了jQuery進(jìn)行組件開(kāi)發(fā)的方法,分享給大家供大家參考,具體如下:
前面的《JavaScript組件開(kāi)發(fā)》分析了JavaScript進(jìn)行組件開(kāi)發(fā)的技巧,這里分析使用jQuery進(jìn)行組件開(kāi)發(fā)的方法。
使用jQuery進(jìn)行組件開(kāi)發(fā)和使用純JavaScript腳本(不使用框架)原理基本類(lèi)似,特別是公共方法的組織是一樣的。
不同點(diǎn)是,jQuery使用了插件機(jī)制,通過(guò)$()直接進(jìn)行操作對(duì)象(DOM元素)綁定,然后對(duì)DOM元素或HTML代碼進(jìn)行綁定事件等的操作。
另一個(gè)不同點(diǎn)則是把jQuery當(dāng)做工具來(lái)使用,用來(lái)創(chuàng)建DOM對(duì)象,快速查找指定DOM對(duì)象等。
例子測(cè)試通過(guò)。
初級(jí)簡(jiǎn)單示例,只實(shí)現(xiàn)了增加頁(yè)和選擇頁(yè)功能。
<!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"> <!--選項(xiàng)卡區(qū)域--> <ul> <li><a href="#tabs1">選項(xiàng)1</a></li> <li><a href="#tabs2">選項(xiàng)2</a></li> <li><a href="#tabs3">選項(xiàng)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; //使用鼠標(biāo)移動(dòng)觸發(fā),亦可通過(guò)click方式觸發(fā)頁(yè)面切換 var defualts = { switchingMode: "mousemove" }; //融合配置項(xiàng) var opts = $.extend({}, defualts, options); //DOM容器對(duì)象,類(lèi)似MX框架中的$e var $e = $(this); //選中的TAB頁(yè)索引 var selectedIndex = 0; //TAB列表 var $lis; //PAGE容器 var aPages = []; //初始化方法 me.init = function(){ //給容器設(shè)置樣式類(lèi) $e.addClass("tabsDiv"); $lis = $("ul li", $e); //設(shè)置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頁(yè) * */ 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!'); } //自動(dòng)執(zhí)行初始化函數(shù) me.init(); //返回函數(shù)對(duì)象 return this; }; })(jQuery); </script> <script type="text/javascript"> /* $(function () { $("#mytabs").tabs; }); */ var tab1 = $("#mytabs").tabs(); tab1.showMsg(); </script> </body> </html>
最終效果如圖所示:
希望本文所述對(duì)大家jQuery程序設(shè)計(jì)有所幫助。
相關(guān)文章
zTree樹(shù)形菜單交互選項(xiàng)卡效果的實(shí)現(xiàn)方法
下面小編就為大家分享一篇zTree樹(shù)形菜單交互選項(xiàng)卡效果的實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2017-12-12使用jQuery判斷Div是否在可視區(qū)域的方法 判斷div是否可見(jiàn)
這篇文章主要介紹了使用jQuery判斷Div是否在可視區(qū)域的方法 判斷div是否可見(jiàn)2016-02-02Jquery ajax 同步阻塞引起的UI線(xiàn)程阻塞問(wèn)題
這篇文章主要介紹了Jquery ajax 同步阻塞引起的UI線(xiàn)程阻塞問(wèn)題的相關(guān)資料,需要的朋友可以參考下2015-11-11jQuery學(xué)習(xí)基礎(chǔ)知識(shí)小結(jié)
jQuery學(xué)習(xí)基礎(chǔ)知識(shí)小結(jié),剛開(kāi)始學(xué)習(xí)jquery的朋友可以參考下。2010-11-11一些主流JS框架中DOMReady事件的實(shí)現(xiàn)小結(jié)
在實(shí)際應(yīng)用中,我們經(jīng)常會(huì)遇到這樣的場(chǎng)景,當(dāng)頁(yè)面加載完成后去做一些事情:綁定事件、DOM操作某些結(jié)點(diǎn)等。2011-02-02jquery ajax 如何向jsp提交表單數(shù)據(jù)
ajax技術(shù)在做表單數(shù)據(jù)傳值非常棒,給用戶(hù)帶來(lái)很好的用戶(hù)體驗(yàn)度,同時(shí),使用jquery可以簡(jiǎn)化開(kāi)發(fā),提高效率。下面給大家介紹jquery ajax 如何向jsp提交表單數(shù)據(jù),需要的朋友可以參考下2015-08-08淺談JQ中mouseover和mouseenter的區(qū)別
下面小編就為大家?guī)?lái)一篇淺談JQ中mouseover和mouseenter的區(qū)別。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。2016-09-09jquery中append()與appendto()用法分析
這篇文章主要介紹了jquery中append()與appendto()用法分析,以實(shí)例的形式分析了jquery中append()與appendto()的具體語(yǔ)法與詳細(xì)用法,需要的朋友可以參考下2014-11-11