jQuery学习全面总结二
来源:互联网 发布:哪种编程语言最简单 编辑:程序博客网 时间:2024/05/05 06:40
1.jQuery事件
一、事件
1、加载DOM
$(document).ready() 这个第一节里详细介绍了
2、事件绑定
jQuery定义了bind()方法作为统一的接口,用来为每一个匹配元素绑定事件处理程序。其基本语法:bind(type,[data],fn);
其中参数type表示事件类型,参数data是可选参数,它可以作为event.data属性值传递给事件对象的额外数据对象。参数fn表示事件处理函数。
例如下例中为p元素绑定单击事件,当点击不同段落文本的时候会提示不同段落文本的内容:
- <span style="font-family:SimSun;font-size:12px;"><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <title> toggle() </title>
- <script src="jquery-2.1.0.min.js" type="text/javascript"></script>
- <script type="text/javascript">
- <!--
- $(function(){
- $("input[type='button']").one("click",function(){
- $(this).val('15210221200');
- })
- })
- //-->
- </script>
- </head>
- <body>
- <input type="button" value="查看联系方式" onclick="">
- </body>
- </html></span>
例如在上面的示例基础上向其传递两个值A和B:
- <span style="font-family:SimSun;font-size:12px;"><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <title> New Document </title>
- <script src="jquery-2.1.0.min.js" type="text/javascript"></script>
- <script type="text/javascript">
- <!--
- $(function(){
- $("p").bind("click",{a:"A",b:"B"},function(event){
- $(this).text(event.data.a+event.data.b);
- })
- })
- //-->
- </script>
- </head>
- <body>
- <p>段落1</p>
- <p>段落2</p>
- <p>段落3</p>
- </body>
- </html></span>
使用one()绑定事件
one() 是bind()的一个特例,由它绑定的事件在执行一次响应之后就失效。看个例子吧:
- <span style="font-family:SimSun;font-size:12px;"><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <title> toggle() </title>
- <script src="jquery-2.1.0.min.js" type="text/javascript"></script>
- <script type="text/javascript">
- <!--
- $(function(){
- $("input[type='button']").one("click",function(){
- alert($(this).val());
- })
- })
- //-->
- </script>
- </head>
- <body>
- <input type="button" value="one绑定测试" onclick="">
- </body>
- </html></span>
对此,jQuery定义了专门注销事件的方法
3、注销事件
unbind(),该方法与bind()是对应的,能够从一个匹配元素中删除所有绑定的事件或者是删除指定的绑定事件。如果没有指定参数,那就删除所有。
参数:
第一个参数是事件类型
第二个参数是要移除的函数
如果没有参数,则删除所有绑定的事件,如果提供了事件类型作为参数,那么只删除该类型的绑定事件
应该不算常用吧。
4、合成事件
jQuery有两个合成事件----hover()和toggle(),这两个方法都是jQuery自定义的方法
(1)、hover()方法
语法结构为 hover(enter,leave)
hover()方法用于模拟光标悬停事件。当光标移动到元素上时,会触发指定的第一个函数,离开时触发第二个函数。
这个函数很好理解,而且实际开发中会有很多妙用。
举个最简单的例子:
- <span style="font-family:SimSun;font-size:12px;"><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <title> hover() </title>
- <script src="jquery-2.1.0.min.js" type="text/javascript"></script>
- <script type="text/javascript">
- <!--
- $(function(){
- $("span").hover(
- function(){
- $("span").text("光标在我身上");
- },
- function(){
- $("span").text("光标跑了~~")
- }
- )
- })
- //-->
- </script>
- </head>
- <body>
- <span >三国杀杀天下</span>
- </body>
- </html></span>
语法结构 toggle(fn1,fn2,fn3...fnN)
toggle()方法用于模拟鼠标连续单击事件,第一次单击事件触发指定的第一个函数,第二次就触发第二个函数.... 如果有更多的函数则依次触发,直到最后一个。随后的每次单击都是对这几个函数的轮番调用。
看到这个方法第一想到的是toggleClass() 用于给元素对象切换样式。
toggle方法也是挺好理解的,以前项目中倒是没有应用过。举个例子:
- <span style="font-family:SimSun;font-size:12px;"><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <title> unbind() </title>
- <script src="jquery-1.8.3.js" type="text/javascript"></script>
- <script type="text/javascript">
- <!--
- $(function(){
- $("input").toggle(
- function(){$("ul").append($("<li><h2>左 慈</h2></li>"))},
- function(){$("ul").append($("<li><h2>曹 冲</h2></li>"))},
- function(){$("ul").append($("<li><h2>神周瑜</h2></li>"))},
- function(){$("ul").append($("<li><h2>神曹操</h2></li>"))},
- function(){$("ul").append($("<li><h2>孙尚香</h2></li>"))},
- function(){$("ul").append($("<li><h2>神关羽</h2></li>"));$(this).unbind()}
- )
- })
- //-->
- </script>
- </head>
- <body>
- <h1>三国杀变态武将</h1>
- <div >
- <ul>
- </ul>
- </div>
- <input type="button" value="连续点击按钮可切换事件" >
- </body>
- </html></span>
6、自定义事件
- <span style="font-family:SimSun;font-size:12px;"><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <title> 自定义事件 </title>
- <script src="jquery-2.1.0.min.js" type="text/javascript"></script>
- <script type="text/javascript">
- <!--
- $(function(){
- $("input").bind("delay",function(event){
- setTimeout(function(){ //延迟执行
- alert(event.type);
- },2000)
- });
- $("input").click(function(){
- $("input").trigger("delay");
- })
- })
- //-->
- </script>
- </head>
- <body>
- <input type="button" value="jQuery自定义事件" >
- </body>
- </html></span>
6、事件对象的属性
引用与事件的目标节点相关的节点。对于mouseover事件来说,他是鼠标指针移到某目标上时所离开的那个节点;对于mouseout来说就是离开目标时鼠标指针要进入到的那个目标altKey表示在声明鼠标事件的时候是否按下了【alt】键。如果返回值为true,则表示按下ctrlKey表示在声明鼠标事件的时候是否按下了【ctrl】键。如果返回值为true,则表示按下
shiftKey表示在声明鼠标事件的时候是否按下了【shift】键。如果返回值为true,则表示按下
metaKey表示在声明鼠标事件的时候是否按下了【meta】键。如果返回值为true,则表示按下
which当在声明mousedown、mouseup时,显示鼠标键的状态值,也就是说哪个鼠标键改变了它的状态,返回值为1,表示按下左键,返回值为2,表示按下中键,返回值为3,表示按下右键which当在声明keydown和keypress事件时,显示触发事件的键盘键的数字编码pageX对于鼠标事件来说,指定鼠标指针相对于页面原点的水平坐标pageY对于鼠标事件来说,指定鼠标指针相对于页面原点的垂直坐标
screenX对于鼠标事件来说,指定鼠标指针相对于屏幕原点的水平坐标
screenY
对于鼠标事件来说,指定鼠标指针相对于屏幕原点的垂直坐标
data存储事件处理函数第二个参数所传递的额外数据preventDefault()取消可能引起任何语义操作的事件,如元素特定事件类型的默认操作,例如取消submit默认的表单提交操作stopPropagation()防止事件沿着DOM树向上传播,可以防止事件冒泡的发生
7、事件冒泡
(1)、什么是事件冒泡
假设网页上有两个元素,其中一个元素嵌套在另一个元素里,并且都绑定了click事件。同事body元素也绑定了click事件。
- <span style="font-family:SimSun;font-size:12px;"><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <title> 事件冒泡 </title>
- <script src="jquery-2.1.0.min.js" type="text/javascript"></script>
- <script type="text/javascript">
- <!--
- $(function(){
- //为input元素绑定click事件
- $('input').click(function(){
- var txt=$('#msg').html()+"<p>div内的按钮被点击.</p>";
- $('#msg').html(txt);
- })
- //为第一个div绑定click事件
- $('div').eq(0).click(function(){
- var txt=$('#msg').html()+"<p>div作用范围发生点击.</p>";
- $('#msg').html(txt);
- })
- //为body元素绑定click事件
- $('body').click(function(){
- var txt=$('#msg').html()+"<p>body元素被单击.</p>";
- $('#msg').html(txt);
- })
- })
- //-->
- </script>
- </head>
- <body >
- <h3>div作用范围外</h3>
- <div id="" class="">
- <h3>div作用范围内</h3>
- <input type="button" value="div内的一个按钮" onclick="">
- <h3>div作用范围内</h3>
- </div>
- <h3>div作用范围外</h3>
- <div id="msg" class="">
- </div>
- </body>
- </html></span>
下面几个图分别是初始页面、点击按钮、点击非按钮的div作用范围、点击div作用范围外的效果图:
之所以称为冒泡,就是因为事件像气泡一样不断向上直到顶部。
(2)、事件冒泡引发的问题:
就像上面那个例子,我们单击了div内按钮,只想触发按钮的单击事件,但是实际结果在我们的预料之外,所以我们必须限定事件的作用范围。
为了解决这个问题,有两个个办法:
A、通过事件对象(event)的stopPropagation()方法
- <span style="font-family:SimSun;font-size:12px;"> //为input元素绑定click事件
- $('input').click(function(event){
- var txt=$('#msg').html()+"<p>div内的按钮被点击.</p>";
- $('#msg').html(txt);
- event.stopPropagation();//停止事件冒泡
- })</span>
B、也可以在事件处理函数执行后加上return false;来防止事件冒泡。
- <span style="font-family:SimSun;font-size:12px;"> //为input元素绑定click事件
- $('input').click(function(){
- var txt=$('#msg').html()+"<p>div内的按钮被点击.</p>";
- $('#msg').html(txt);
- return false;
- })</span>
live()与die()
例子:
- <span style="font-family:SimSun;font-size:12px;"><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <title> 事件委派与解除委派 </title>
- <script src="jquery-1.8.3.js" type="text/javascript"></script>
- <script type="text/javascript">
- <!--
- $(function(){
- $("p").live("click",function(){
- $(this).after("<p>段落文字</p>");
- })
- $("input").click(function(){
- $("p").die();//不带参数就是解除live事件,如果带了参数type,那会移除相应的live事件,如果同时指定了第二个参数,则只能移除指定事件的处理函数。
- })
- })
- //-->
- </script>
- </head>
- <body >
- <input type="button" value="解除事件绑定">
- <p>段落文本</p>
- </body>
- </html></span>
jQuery支持事件命名空间以方便管理事件。例如,在下面示例中为div元素绑定多个事件类型,然后使用命名空间进行规范,从而方便管理。所谓事件命名空间,就是在事件类型后面以点语法附加一个别名,以便于引用事件,如,click.a ,其中a就是click当前事件的别名。
如下示例:
- <span style="font-family:SimSun;font-size:12px;"><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <title> jQuery命名空间 </title>
- <script src="jquery-2.1.0.min.js" type="text/javascript"></script>
- <script type="text/javascript">
- <!--
- $(function(){
- $("div").bind("click.a",function(){
- $("body").append("<p>click事件<p/>")
- });
- $("div").bind("dblclick.a",function(){
- $("body").append("<p>dblclick事件<p/>")
- });
- $("div").bind("mouseover.a",function(){
- $("body").append("<p>mouseover<p/>")
- });
- $("div").bind("mouseout.a",function(){
- $("body").append("<p>mouseout<p/>")
- });
- })
- //-->
- </script>
- </head>
- <body >
- <div id="" class="">
- jQuery命名空间
- </div>
- </body>
- </html></span>
$("div").unbind(".a");
同样,假如我们为click事件类型指定了不同的命名空间,那么当我们只想删一个的时候只要指定它的命名空间即可。
10、绑定多个事件
我们可以为同一元素绑定多个事件,既可以是同类型的也可以是不同类型的。
2.jQuery动画
1、最简单的动画:显隐效果
CSS支持两种方法显示和隐藏元素,即使用visibility或display样式,他们控制元素显示和隐藏的时候效果相同,但是结果却不同。
具体说明如下:
visibility 属性在隐藏元素的时候,同时会保存元素在文档流中的影响力,隐藏后元素的未知保持不变。该属性包括visible(默认)和hidden两个值。
display 隐藏后,隐藏的元素不再占用文档的位置。
注:这块比较巧,这个样式属性正好帮了我一个忙,事情是这样的:
在这个页面中有设备类型的列表框和抓包网口的多选框,要求选择设备类型选择千兆的时候显示4个抓包网口,选择万兆的时候隐藏掉eth3和eth4两个网口。
我当时第一做法就是把eth3和eth4用span元素包裹,然后选择万兆的时候触发事件让span执行hide()方法。这样就把后两个网口隐藏了,但是有一个问题,当hide()后,页面结
构变乱了,测试部又说不能用disabled="disabled"只读属性代替,正想找个时间让美工帮调一下。晚上看会书就看到了visibility 这个样式属性,正好就用上了。
在jQuery中定义了show()和hide()两种方法进行元素的索引。其实这俩方法内部就是对元素的display样式进行操作。
show()和hide()方法还可以设置参数,第一个参数代表(隐藏)的速度,参数值越小越快(可以是具体的值,如1000代表1000ms,也可以是预定义的字符串:slow、normal、st,分
别代表600 400 和200),第二个参数也是一个可选参数,表示在动画演示完毕要调用的回调函数。
示例:
- <span style="font-family:SimSun;font-size:12px;"><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <title> show和hide </title>
- <script src="jquery-2.1.0.min.js" type="text/javascript"></script>
- <script type="text/javascript">
- <!--
- $(function(){
- var t=false;
- $("input").click(function(){
- if(t){
- $("div").show(1000,function(){
- $("input").val('隐藏元素');
- })
- t=false;
- }
- else{
- $("div").hide(1000,function(){
- $("input").val('显示元素');
- })
- t=true;
- }
- })
- })
- //-->
- </script>
- </head>
- <body >
- <input type="button" value="隐藏元素" >
- <div id="" class=""><h1>div元素</h1></div>
- </body>
- </html></span>
- <span style="font-family:SimSun;font-size:12px;"><!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>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title>Panel</title>
- <style type="text/css">
- *{margin:0;padding:0;}
- body { font-size: 13px; line-height: 130%; padding: 60px }
- #panel { width: 300px; border: 1px solid #0050D0 }
- .head { height:24px;line-height:24px;text-indent:10px;background: #96E555; cursor: pointer;width:100%; }
- .content { padding: 10px; text-indent:24px; border-top: 1px solid #0050D0;display:block; }
- </style>
- <script src="../../scripts/jquery.js" type="text/javascript"></script>
- <script type="text/javascript">
- $(function(){
- $("#panel h5.head").toggle(function(){
- $(this).next().hide(600);
- },function(){
- $(this).next().show(600);
- })
- })
- </script>
- </head>
- <body>
- <div id="panel">
- <h5 class="head">三国杀杀天下</h5>
- <div class="content">
- 夏侯惇的眼睛,陆逊的联营,郭嘉司马深深的基情
- </div>
- </div>
- </body>
- </html></span>
fadeIn()和fadeOut()方法,这两个方法是在一定时间内改变元素的不透明度,直到元素完全消失(或者出现),语法与show()和hide()相同。
- <span style="font-family:SimSun;font-size:12px;"><!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>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title>Panel</title>
- <style type="text/css">
- *{margin:0;padding:0;}
- body { font-size: 13px; line-height: 130%; padding: 60px }
- #panel { width: 300px; border: 1px solid #0050D0 }
- .head { height:24px;line-height:24px;text-indent:10px;background: #96E555; cursor: pointer;width:100%; }
- .content { padding: 10px; text-indent:24px; border-top: 1px solid #0050D0;display:block; }
- </style>
- <script src="../../scripts/jquery.js" type="text/javascript"></script>
- <script type="text/javascript">
- $(function(){
- $("#panel h5.head").toggle(function(){
- $(this).next().fadeOut(1000,function(){
- alert('回调函数')
- });
- },function(){
- $(this).next().fadeIn(1000);
- })
- })
- </script>
- </head>
- <body>
- <div id="panel">
- <h5 class="head">三国杀杀天下</h5>
- <div class="content">
- 夏侯惇的眼睛,陆逊的联营,郭嘉司马深深的基情
- </div>
- </div>
- </body>
- </html></span>
slideUp()和slideDown()方法 用法与上两组函数相同
- <span style="font-family:SimSun;font-size:12px;"><!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>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title>Panel</title>
- <style type="text/css">
- *{margin:0;padding:0;}
- body { font-size: 13px; line-height: 130%; padding: 60px }
- #panel { width: 300px; border: 1px solid #0050D0 }
- .head { height:24px;line-height:24px;text-indent:10px;background: #96E555; cursor: pointer;width:100%; }
- .content { padding: 10px; text-indent:24px; border-top: 1px solid #0050D0;display:block; }
- </style>
- <script src="../../scripts/jquery.js" type="text/javascript"></script>
- <script type="text/javascript">
- $(function(){
- $("#panel h5.head").toggle(function(){
- $(this).next().slideUp(1000,function(){
- alert('回调函数');
- });
- },function(){
- $(this).next().slideDown();
- })
- })
- </script>
- </head>
- <body>
- <div id="panel">
- <h5 class="head">三国杀杀天下</h5>
- <div class="content">
- 夏侯惇的眼睛,陆逊的联营,郭嘉司马深深的基情
- </div>
- </div>
- </body>
- </html></span>
animate(params,speed,callback)
参数说明:
params:一个包含样式属性及值的映射
sleep:速度参数,可选
callback:在动画完成时执行的函数
(1)、一个最简单的自定义动画
- <span style="font-family:SimSun;font-size:12px;"><!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>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title>Panel</title>
- <style type="text/css">
- *{margin:0;padding:0;}
- body { padding: 60px }
- #panel {position: relative; width: 100px; height:100px;border: 1px solid #0050D0;background: #96E555; cursor: pointer}
- </style>
- <script src="../../scripts/jquery.js" type="text/javascript"></script>
- <script type="text/javascript">
- $(function(){
- $("#panel").click(function(){
- $(this).animate({left: "500px"}, 3000);
- })
- })
- </script>
- </head>
- <body>
- <div id="panel"></div>
- </body>
- </html></span>
属性设置成'relative'或者'sbsolude'。
(2)、累加累减动画
上个例子当中,第一次单击div会左移500像素,第二次单击就不会动了。累加累减就是连续动的,只需要将left:"500px"改成left:"+=500px"或者left:"-=500px"即可。
- <span style="font-family:SimSun;font-size:12px;">$(function(){
- $("#panel").click(function(){
- $(this).animate({left: "+=500px"}, 3000);
- })
- })</span>
A、同时执行多个动画
上面的例子只控制了left属性的变化,这回我们在控制left属性的时候同时控制元素高度变为200px
- <span style="font-family:SimSun;font-size:12px;">$(function(){
- $("#panel").click(function(){
- $(this).animate({left: "500px",height:"200px"}, 3000);
- })
- })</span>
上面例子中元素右移和放大高度两个动画是同时进行的。现在我们要实现先右移再放大高度,那很简单,只需要把上面的animate()方法拆开写成两个即可
- <span style="font-family:SimSun;font-size:12px;">$(function(){
- $("#panel").click(function(){
- $(this).animate({left: "500px"},3000)
- .animate({height:"200px"},1000);
- })
- })</span>
接下来完成更复杂的动画。单击div元素后让他向右移动的同时增大他的高度,并将它的不透明度从50%切换到100%。然后再让它从上向下移动,同时它的宽度变大,当完成这
些效果后让它以淡出的方式隐藏掉。
- <span style="font-family:SimSun;font-size:12px;">$(function(){
- $("#panel").css("opacity",0.5);//设置不透明度
- $("#panel").click(function(){
- $(this).animate({left: "400px",height:"200px",opacity:"1"},3000)
- .animate({top:"200px",width:"200px"},3000)
- .fadeOut(1000);
- })
- })</span>
在上例中,如果想在最后一步切换css样式而不是隐藏元素。这我们就可以用到animate的第三个参数回调函数了
- <span style="font-family:SimSun;font-size:12px;">$(function(){
- $("#panel").css("opacity",0.5);//设置不透明度
- $("#panel").click(function(){
- $(this).animate({left: "400px",height:"200px",opacity:"1"},3000)
- .animate({top:"200px",width:"200px"},3000,function(){$(this).css("border","5px solid blue")});
- })
- })</span>
5、停止动画和判断是否处于动画状态
(1)、停止元素的动画
stop([clearQueue][,gotoEnd]) 两个都是可选参数,都是boolean类型
参数说明:
clearQueue:代表是否清空未执行完的动画队列
gotoEnd :代表是否将正在执行的动画跳到末状态
通过一个综合的示例就可以弄明白stop()方法的这两个参数了:
- <span style="font-family:SimSun;font-size:12px;"><!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>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title>Panel</title>
- <style type="text/css">
- *{margin:0;padding:0;}
- body { font-size: 13px; line-height: 130%; padding: 60px }
- #panel { width: 60px; border: 1px solid #0050D0 ;height:22px;overflow:hidden;}
- .head { padding: 5px; background: #96E555; cursor: pointer;width: 300px;}
- .content { padding: 10px; text-indent: 2em; border-top: 1px solid #0050D0;display:block; width:280px;}
- </style>
- <script src="../../../scripts/jquery.js" type="text/javascript"></script>
- <script type="text/javascript">
- $(function(){
- $("button:eq(0)").click(function () {
- $("#panel")
- .animate({height : "150" } , 2000 )
- .animate({width : "300" } , 2000 )
- .hide(3000)
- .animate({height : "show" , width : "show" , opacity : "show" } , 2000 )
- .animate({height : "500"} , 2000 );
- });
- $("button:eq(1)").click(function () {
- $("#panel").stop();//停止当前动画,继续下一个动画
- });
- $("button:eq(2)").click(function () {
- $("#panel").stop(true);//清除元素的所有动画
- });
- $("button:eq(3)").click(function () {
- $("#panel").stop(false,true);//让当前动画直接到达末状态 ,继续下一个动画
- });
- $("button:eq(4)").click(function () {
- $("#panel").stop(true,true);//清除元素的所有动画,让当前动画直接到达末状态
- });
- })
- </script>
- </head>
- <body>
- <button>开始一连串动画</button>
- <button>stop()</button>
- <button>stop(true)</button>
- <button>stop(false,true)</button>
- <button>stop(true,true)</button>
- <div id="panel">
- <h5 class="head">三国杀杀天下</h5>
- <div class="content">
- 夏侯惇的眼睛,陆逊的联营,郭嘉司马深深的基情
- </div>
- </div>
- </body>
- </html></span>
当使用animate()方法的时候,要避免动画的累积导致的动画与用户的行为不一致的现象。当用户快速在某个元素上执行animate()动画时,就会出现动画累积。
解决办法是判断元素是否正在处于动画状态,当不处于动画状态的时候,才为元素添加新的动画。
用法:
- <span style="font-family:SimSun;font-size:12px;">if(!$(element).is(":animated")){
- //添加新的动画
- }</span>
除了上面提到的动画方法,jquery中还有3个专门用于交互的动画方法
toggle(speed[,callback]) 切换元素可见状态,用来代替hide()和show()的。
slideToggle(speed[,callback])通过调整元素高度来切换可见状态,代替slideUp和slideDown的。
fadeTo(speed,opacity[,callback])将元素的不透明度渐变的调整到某一值。
3.jQuery几个经典表单应用
1、文本框获得(失去)焦点
当文本框获得输入焦点时,将该文本框高亮显示,算不得一个应用,只是一个小技巧,可以提高用户体验。
- <span style="font-family:SimSun;font-size:12px;"><!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>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title></title>
- <style type="text/css">
- body{
- font:normal 12px/17px Arial;
- }
- div{
- padding:2px;
- }
- input, textarea {
- width: 12em;
- border: 1px solid #888;
- }
- .focus {
- border: 1px solid #f00;
- background: #fcc;
- }
- </style>
- <!-- 引入jQuery -->
- <script src="jquery-2.1.0.min.js" type="text/javascript"></script>
- <script type="text/javascript">
- $(function(){
- $(":input").focus(function(){
- $(this).addClass("focus");
- if($(this).val() ==this.defaultValue){
- $(this).val("");
- }
- }).blur(function(){
- $(this).removeClass("focus");
- if ($(this).val() == '') {
- $(this).val(this.defaultValue);
- }
- });
- })
- </script>
- </head>
- <body>
- <form action="" method="post" id="regForm">
- <fieldset>
- <legend>个人基本信息</legend>
- <div>
- <label for="username">名称:</label>
- <input id="username" type="text" value="名称" />
- </div>
- <div>
- <label for="pass">密码:</label>
- <input id="pass" type="password" value="密码" />
- </div>
- <div>
- <label for="msg">详细信息:</label>
- <textarea id="msg" rows="2" cols="20">详细信息</textarea>
- </div>
- </fieldset>
- </form>
- </body>
- </html></span>
效果图:
2、Elastic弹性文本域
Elastic是一款功能专一的表单插件,他可以控制页面内表单域(<textarea>)标签高度自动伸缩,以适应包含的文本。应用这个插件的时候页面需要引入jquery.elastic.source.js。
插件下载地址:点击进入下载页面
- <span style="font-family:SimSun;font-size:12px;"><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <title> New Document </title>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <script src="jquery-2.1.0.min.js" type="text/javascript"></script>
- <script src="jquery.elastic.source.js" type="text/javascript" ></script>
- <script type="text/javascript">
- //页面加载方法
- $(function(){
- $("textarea").elastic();//应用弹性文本框
- })
- </script>
- </head>
- <body>
- <textarea name="" rows="2" cols="43">
- 沁园春·雪
- 北国风光,千里冰封,万里雪飘。
- 望长城内外,惟余莽莽;大河上下,顿失滔滔。
- 山舞银蛇,原驱蜡象,欲与天公试比高。
- 须晴日,看红装素裹,分外妖娆。
- 江山如此多娇,引无数英雄竞折腰。
- 惜秦皇汉武,略输文采;唐宗宋祖,稍逊风骚。
- 一代天骄,成吉思汗,只识弯弓射大雕。
- 俱往矣,数风流人物,还看今朝。
- </textarea>
- </body>
- </html></span>
效果图:
我们最初设置的<textarea>标签的rows属性值为2 ,随着文本内容的增多高度会自动增加,当然了,随着内容的减少也可以高度降低的。
3、Autotab自动Tab文本框
Autotab也是一款功能专一的表单插件,它提供了自动跳格的功能,当用户输入的字符数一旦超过已定义的最大长度,则会根据事先设置的目标自动跳转到相应元素上,省却了
用户按【Tab】键的麻烦。最典型的应用就是输入IP地址、软件激活码等地方了,我们做的web项目中也有很多地方可以用到这插件,对于提高用户体验还是很有帮助的。
使用时需要引入jquery.autotab.js,下载地址:点击进入下载页面
- <span style="font-family:SimSun;font-size:12px;"><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <title> New Document </title>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <script src="jquery-2.1.0.min.js" type="text/javascript"></script>
- <script src="jquery.autotab.js" type="text/javascript"></script>
- <script type="text/javascript">
- //页面加载方法
- $(function(){
- $('#autotab').submit(function(){
- return false;
- })
- $('#autotab :input').autotab_magic();//为页面文本框绑定autotab插件
- })
- </script>
- </head>
- <body>
- <h1>jQuery整理笔记七</h1>
- <h2>Autotab自动Tab文本框</h2>
- <form method="post" action="" id="autotab">
- <label>请输入验证码:
- <input type="text" name="num1" id="num1" maxlength="3" size="3">
- <input type="text" name="num2" id="num2" maxlength="3" size="3">
- <input type="text" name="num3" id="num3" maxlength="3" size="3">
- <input type="text" name="num4" id="num4" maxlength="3" size="3">
- <input type="text" name="num5" id="num5" maxlength="3" size="3">
- <input type="text" name="num6" id="num6" maxlength="3" size="3">
- </form>
- </body>
- </html></span>
除了可以限定输入长度外,还可以通过autotab_filter()方法限定输入的字符类型,这个方法还能过滤大写、小写、空格、字母等,具体的用到了现查吧。
如果将上面的js改成:
- <span style="font-family:SimSun;font-size:12px;">$(function(){
- $('#autotab').submit(function(){
- return false;
- });
- $('#autotab :input').autotab_magic().autotab_filter('numeric');
- })</span>
4、passwordStrength密码强度指标
passwordStrength插件能够根据用户输入的密码,以图形化方式显示密码的强度。
- <span style="font-family:SimSun;font-size:12px;"><!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>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title>passwordStrength</title>
- <link href="css/style.css" rel="stylesheet" type="text/css" media="all" />
- <script type="text/javascript" src="jquery-2.1.0.min.js"></script>
- <script type="text/javascript" src="passwordStrength.js"></script>
- <script type="text/javascript">
- $(function(){
- $('input[name="password"]').passwordStrength();
- })
- </script>
- <style type="text/css">
- .is0{background:url(images/progressImg1.png) no-repeat 0 0;width:138px;height:7px;margin:10px 0 0 104px;}
- .is10{background-position:0 -7px;}
- .is20{background-position:0 -14px;}
- .is30{background-position:0 -21px;}
- .is40{background-position:0 -28px;}
- .is50{background-position:0 -35px;}
- .is60{background-position:0 -42px;}
- .is70{background-position:0 -49px;}
- .is80{background-position:0 -56px;}
- .is90{background-position:0 -63px;}
- .is100{background-position:0 -70px;}
- #autotab input { width:138px; }
- </style>
- </head>
- <body>
- <h1>jQuery整理笔记七</h1>
- <h2>表单开发(Forms)</h2>
- <hr />
- <h3>passwordStrength密码强度指标</h3>
- <form action="" method="post" id="autotab" class="p1">
- <label>请输入密码:
- <input type="password" name="password" />
- <div id="passwordStrengthDiv" class="is0"></div>
- </label>
- </form>
- </body>
- </html></span>
执行效果图:
5、formToWizard表单填充向导
这个是什么意思呢?其实我们实际见的也很多,有很多网站填写注册信息的时候是分步进行的,比方说,先填写个人信息,然后再填写工作信息...然后一起提交。这就避免了庞
大的信息量都在一个页面上进行填写。
formToWizard就是解决这个问题的一个小插件,插件下载地址:点击进入下载页面
- <span style="font-family:SimSun;font-size:12px;"><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <title> New Document </title>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <script src="jquery-2.1.0.min.js" type="text/javascript"></script>
- <script type="text/javascript" src="formToWizard.js"></script>
- <script type="text/javascript">
- //页面加载方法
- $(function(){
- $("#form1").formToWizard({ submitButton: 'SaveAccount' })
- })
- </script>
- <style type="text/css">
- #wrap { margin:1em 4em; font-size:12px; padding:1em 1em; border:solid 1px #fff; }
- fieldset { border:none; width:320px; }
- legend { font-size:18px; margin:0px; padding:10px 0px; color:#b0232a; font-weight:bold; }
- label { display:block; margin:15px 0 5px; }
- input[type=text], input[type=password] { width:300px; padding:5px; border:solid 1px #000; }
- .prev, .next { background-color:#b0232a; padding:5px 10px; color:#fff; text-decoration:none; }
- .prev:hover, .next:hover { background-color:#000; text-decoration:none; }
- .prev { float:left; }
- .next { float:right; }
- #steps { list-style:none; width:100%; overflow:hidden; margin:0px; padding:0px; }
- #steps li { font-size:24px; float:left; padding:10px; color:#b0b1b3; }
- #steps li span { font-size:11px; display:block; }
- #steps li.current { color:#000; }
- #makeWizard { background-color:#b0232a; color:#fff; padding:5px 10px; text-decoration:none; font-size:18px; }
- #makeWizard:hover { background-color:#000; }
- </style>
- </head>
- <body>
- <div id="wrap">
- <form id="form1" action="">
- <fieldset>
- <legend>登录信息</legend>
- <label for="Name">昵称</label>
- <input id="Name" type="text" />
- <label for="Email">Email</label>
- <input id="Email" type="text" />
- <label for="Password">密码</label>
- <input id="Password" type="password" />
- </fieldset>
- <fieldset>
- <legend>公司信息</legend>
- <label for="CompanyName">公司名称</label>
- <input id="CompanyName" type="text" />
- <label for="Website">公司网址</label>
- <input id="Website" type="text" />
- <label for="CompanyEmail">公司邮箱</label>
- <input id="CompanyEmail" type="text" />
- </fieldset>
- <fieldset>
- <legend>个人信息</legend>
- <label for="NameOnCard">真实姓名</label>
- <input id="NameOnCard" type="text" />
- <label for="CardNumber">身份证号</label>
- <input id="CardNumber" type="text" />
- <label for="CreditcardMonth">发卡日期</label>
- <select id="CreditcardMonth">
- <option value="1">1</option>
- <option value="2">2</option>
- <option value="3">3</option>
- <option value="4">4</option>
- <option value="5">5</option>
- <option value="6">6</option>
- <option value="7">7</option>
- <option value="8">8</option>
- <option value="9">9</option>
- <option value="10">10</option>
- <option value="11">11</option>
- <option value="12">12</option>
- </select>
- <select id="CreditcardYear">
- <option value="2009">2009</option>
- <option value="2010">2010</option>
- <option value="2011">2011</option>
- </select>
- <label for="Address1">地址1</label>
- <input id="Address1" type="text" />
- <label for="Address2">地址2</label>
- <input id="Address2" type="text" />
- <label for="City">城市</label>
- <input id="City" type="text" />
- <label for="Country">国家</label>
- <select id="Country">
- <option value="CA">Canada</option>
- <option value="US">United States of America</option>
- <option value="GB">United Kingdom (Great Britain)</option>
- <option value="AU">Australia</option>
- <option value="JP">Japan</option>
- </select>
- </fieldset>
- <div>
- <input id="SaveAccount" type="button" value="提交表单" />
- </div>
- </div>
- </form>
- </body>
- </html>
- </span>
效果图:
6、checkbox复选框(全选反选等操作)
以前经常用,不说了。
7、下拉框的应用
这回先看个图:
大家肯定都见过类似效果的网页,怎么实现的呢,代码很简单:
- <span style="font-family:SimSun;font-size:12px;"><!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>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title></title>
- <style type="text/css">
- * { margin:0; padding:0; }
- div.centent {
- float:left;
- text-align: center;
- margin: 10px;
- }
- span {
- display:block;
- margin:2px 2px;
- padding:4px 10px;
- background:#898989;
- cursor:pointer;
- font-size:12px;
- color:white;
- }
- </style>
- <!-- 引入jQuery -->
- <script src="jquery-2.1.0.min.js" type="text/javascript"></script>
- <script type="text/javascript">
- $(function(){
- //移到右边
- $('#add').click(function() {
- //获取选中的选项,删除并追加给对方
- $('#select1 option:selected').appendTo('#select2');
- });
- //移到左边
- $('#remove').click(function() {
- $('#select2 option:selected').appendTo('#select1');
- });
- //全部移到右边
- $('#add_all').click(function() {
- //获取全部的选项,删除并追加给对方
- $('#select1 option').appendTo('#select2');
- });
- //全部移到左边
- $('#remove_all').click(function() {
- $('#select2 option').appendTo('#select1');
- });
- //双击选项
- $('#select1').dblclick(function(){ //绑定双击事件
- //获取全部的选项,删除并追加给对方
- $("option:selected",this).appendTo('#select2'); //追加给对方
- });
- //双击选项
- $('#select2').dblclick(function(){
- $("option:selected",this).appendTo('#select1');
- });
- });
- </script>
- </head>
- <body>
- <div class="centent">
- <select multiple="multiple" id="select1" style="width:100px;height:160px;">
- <option value="1">曹操</option>
- <option value="2">曹昂</option>
- <option value="3">曹丕</option>
- <option value="4">曹彰</option>
- <option value="5">曹植</option>
- <option value="6">曹熊</option>
- <option value="7">曹仁</option>
- <option value="8">曹洪</option>
- <option value="9">曹休</option>
- <option value="10">曹真</option>
- <option value="11">曹爽</option>
- </select>
- <div>
- <span id="add" >选中添加到右边>></span>
- <span id="add_all" >全部添加到右边>></span>
- </div>
- </div>
- <div class="centent">
- <select multiple="multiple" id="select2" style="width: 100px;height:160px;">
- <option value="12">曹芳</option>
- </select>
- <div>
- <span id="remove"><<选中删除到左边</span>
- <span id="remove_all"><<全部删除到左边</span>
- </div>
- </div>
- </body>
- </html></span>
代码实现的功能:
1)、将选中的选项添加给对方
2)、将全部选项添加给对方
3)、双击某个选项将其添加给对方
- jQuery学习全面总结二
- jQuery学习全面总结一
- jQuery学习总结(二)
- jQuery学习总结(二)
- jQuery学习总结(二)
- jQuery选择器全面总结
- jQuery的全面学习
- jquery学习总结(二) 操作DOM
- C#JQuery学习(二)技巧总结
- JQuery选择器学习总结(二)
- System.IO全面总结(二)
- System.IO全面总结(二)
- Log4j使用总结(全面)二
- 传智播客--jQuery的全面学习
- jQuery学习大总结(二)jQuery选择器完整介绍
- Jquery学习总结(二) jquery选择器详解
- Sqlite全面学习(二)
- SQLite全面学习(二)
- android 使用RadioGroup模拟 ViewpagerIndicator
- 第一章例题17年龄排序学UVa11462(内存受限问题,计数排序)
- 职场分享:职场中如何提升我们的沟通表达能力
- 今日作息及食谱(7.24)
- Hibernate关联关系映射(一对一关联)
- jQuery学习全面总结二
- [教程] CSS的优化
- 极简单的员工工资系统
- 大数据时代的机器学习
- Unity3d + NGUI 的多分辨率适配
- HDOJ 题目1715 大菲薄数 大数
- crontab
- Vision引擎组件系统中玩家角色与区块阴影介绍
- AES加解密在Android 4.2上的Encryption error