接下来几篇教程,将重点讲解jquery1.4新增的16个方法,有几个方法还是非常实用的。
.clearQueue():移除队列中还没有运行的所有函数
clearQueue()的作用与stop(true)很类似,简化了stop(true),在1.4后stop()主要用于终止动画,而终止队列函数使用clearQueue(),clearQueue()接受一个参数:队列名称,即移除特定队列。
来看demo:http://www.36ria.com/demo/jquery1.4测试/clearQueue.html
源代码如下:
- <input name="" id="start" type="button" value="开始运行动画" />
- <input name="" id="stop" type="button" value="终止动画" />
- <div></div>
- $(function(){
- $("#start").click(function () {
- $("div").show("slow");
- $("div").animate({left:'+=200'},5000);
- $("div").queue(function () {
- $(this).addClass("newcolor");
- $(this).dequeue();
- });
- $("div").animate({left:'-=200'},1500);
- $("div").queue(function () {
- $(this).removeClass("newcolor");
- $(this).dequeue();
- });
- $("div").slideUp();
- });
- $("#stop").click(function () {
- $("div").clearQueue();
- $("div").stop();
- });
- })
留意stop的监听函数中的
$(“div”).clearQueue();
$(“div”).stop();
大家可以看看将 这二句其中一句注释掉后,看下效果,体会下clearQueue与stop的区别。
.contains():检查一个DOM元素是否包含另外一个DOM元素
留意contains接受二个参数是DOM元素,如下形式:
- jQuery.contains(document.documentElement, document.body); // true
- jQuery.contains(document.body, document.documentElement); // false
.delay():设置一个定时器,用于延迟队列中函数的运行
接受二个参数:
- 第一个参数:用于定时器的持续时间
- 第二个参数:对列名(可选)
来看demo:http://www.36ria.com/demo/jquery1.4测试/delay.html
源代码如下:
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title>delay()</title>
- <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
- <style>
- div { width: 60px; height: 60px; float: left; }
- .first { background-color: #3f3; }
- .second { background-color: #33f;}
- </style>
- </head>
- <body>
- <p><button>运行</button></p>
- <div class="first"></div>
- <div class="second"></div>
- <script>
- $("button").click(function() {
- $("div.first").slideUp(300).delay(800).fadeIn(400);
- $("div.second").slideUp(300).fadeIn(400);
- });
- </script>
- </body>
- </html>
留意第一个绿色的层,在隐藏后,延迟了800毫秒才又触发fadeIn显示。
.detach():用于删除对象,同时保留删除对象数据。
detach这个方法非常有用,作用近似于.remove(),但比remove来的强大。detach在删除的同时,会返回被删除对象。
来看demo:http://www.36ria.com/demo/jquery1.4测试/detach.html
源代码如下:
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title>detach()</title>
- <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
- <style>p { background:yellow; margin:6px 0; } p.off { background: black; }</style>
- </head>
- <body>
- <button>添加/删除段落</button>
- <p>这是一个用于测试的段落</p>
- <script>
- var p;
- $("button").click(function(){
- if (p) {
- p.appendTo("body");
- p = null;
- } else {
- p = $("p").detach();
- }
- });
- </script>
- </body>
- </html>
留意p = $(“p”).detach();。删除了$(“p”)。但将$(“p”)写入了p 这个变量。
所有教程结束了,将把所有demo压缩下放出。

