使用jquery进行组件开发和使用纯javascript脚本(不使用框架)原理基本类似,特别是公共方法的组织是一样的。
不同点是,jquery使用了插件机制,通过$()直接进行操作对象(dom元素)绑定,然后对dom元素或html代码进行绑定事件等的操作。
另一个不同点则是把jquery当做工具来使用,用来创建dom对象,快速查找指定dom对象等。
例子测试通过。
初级简单示例,只实现了增加页和选择页功能。
<!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="mx/scripts/lib/jquery.js" type="text/javascript"></script>
<link href="tabs.css" rel="stylesheet" type="text/css" />
<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>
</head>
<body>
<!--
<p style="width:400px;height:100px;border:solid 1px #e0e0e0;">
</p>
-->
<!--tabs示例-->
<p id="mytabs">
<!--选项卡区域-->
<ul>
<li><a href="#tabs1">选项1</a></li>
<li><a href="#tabs2">选项2</a></li>
<li><a href="#tabs3">选项3</a></li>
</ul>
<!--面板区域-->
<p id="tabs1">11111</p>
<p id="tabs2">22222</p>
<p id="tabs3">33333</p>
</p>
<script lang="javascript">
(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 = $('p', $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!');
}
//自动执行初始化函数
me.init();
//返回函数对象
return this;
};
})(jquery);
</script>
<script type="text/javascript">
/*
$(function () {
$("#mytabs").tabs;
});
*/
var tab1 = $("#mytabs").tabs();
tab1.showmsg();
</script>
</body>
</html>
最终效果如图所示:
以上就是使用jquery进行组件开发完整例子详解的详细内容。