AI编程助手
AI免费问答

使用jQuery进行组件开发完整例子详解

黄舟   2017-03-18 15:01   1797浏览 原创

使用jquery进行组件开发和使用纯javascript脚本(不使用框架)原理基本类似,特别是公共方法的组织是一样的。

不同点是,jQuery使用了插件机制,通过$()直接进行操作对象(DOM元素)绑定,然后对DOM元素或HTML代码进行绑定事件等的操作。

另一个不同点则是把jQuery当做工具来使用,用来创建DOM对象,快速查找指定DOM对象等。

例子测试通过。

初级简单示例,只实现了增加页和选择页功能。

nbsp;html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<title> Design JS component with jQuery </title><script></script><link>

 <style>
      
.tabsp{width: 500px;height: 350px;margin-top: 0px;margin-left: 0px;}
.tabsp 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;
}

.tabsp p{
	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>   



<!--
	<p style="width:400px;height:100px;border:solid 1px #e0e0e0;">
	
	</p>
-->

    <!--tabs示例-->
    <p>
        <!--选项卡区域-->
        </p>
                
  • 选项1
  •             
  • 选项2
  •             
  • 选项3
  •         
                 

11111

        

22222

        

33333

          <script> (function ($) { $.fn.tabs = function (options) { var me = this; //使用鼠标移动触发,亦可通过click方式触发页面切换 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("tabsp"); $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"); //$("p", $e).not(":first").hide(); //TAB pages绑定 var $pages = $(&#39;p&#39;, $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(&#39;WAHAHA!&#39;); } //自动执行初始化函数 me.init(); //返回函数对象 return this; }; })(jQuery); </script>    <script> /* $(function () { $("#mytabs").tabs; }); */ var tab1 = $("#mytabs").tabs(); tab1.showMsg(); </script> 

最终效果如图所示:


声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。