jquery1.4教程三:新增方法教程(1)

发布于,归属于jquery教程沙发还空着,抢! 共有260人围观    

接下来几篇教程,将重点讲解jquery1.4新增的16个方法,有几个方法还是非常实用的。

.clearQueue():移除队列中还没有运行的所有函数

clearQueue()的作用与stop(true)很类似,简化了stop(true),在1.4后stop()主要用于终止动画,而终止队列函数使用clearQueue(),clearQueue()接受一个参数:队列名称,即移除特定队列。

来看demo:http://www.36ria.com/demo/jquery1.4测试/clearQueue.html

源代码如下:

  1. <input name="" id="start" type="button" value="开始运行动画" />
  2. <input name="" id="stop" type="button" value="终止动画" />
  3. <div></div>
  1. $(function(){
  2.     $("#start").click(function () {
  3.       $("div").show("slow");
  4.       $("div").animate({left:'+=200'},5000);
  5.       $("div").queue(function () {
  6.         $(this).addClass("newcolor");
  7.         $(this).dequeue();
  8.       });
  9.       $("div").animate({left:'-=200'},1500);
  10.       $("div").queue(function () {
  11.         $(this).removeClass("newcolor");
  12.         $(this).dequeue();
  13.       });
  14.       $("div").slideUp();
  15.     });
  16.     $("#stop").click(function () {
  17.       $("div").clearQueue();
  18.       $("div").stop();
  19.     });   
  20. })

留意stop的监听函数中的
$(“div”).clearQueue();
$(“div”).stop();
大家可以看看将 这二句其中一句注释掉后,看下效果,体会下clearQueue与stop的区别。

.contains():检查一个DOM元素是否包含另外一个DOM元素

留意contains接受二个参数是DOM元素,如下形式:

  1. jQuery.contains(document.documentElement, document.body); // true
  2. jQuery.contains(document.body, document.documentElement); // false

.delay():设置一个定时器,用于延迟队列中函数的运行

接受二个参数:

  1. 第一个参数:用于定时器的持续时间
  2. 第二个参数:对列名(可选)

来看demo:http://www.36ria.com/demo/jquery1.4测试/delay.html
源代码如下:

  1. <html xmlns="http://www.w3.org/1999/xhtml">
  2. <head>
  3. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  4. <title>delay()</title>
  5. <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
  6.  
  7.   <style>
  8. div { width: 60px; height: 60px; float: left; }
  9. .first { background-color: #3f3; }
  10. .second { background-color: #33f;}
  11. </style>
  12. </head>
  13.  
  14. <body>
  15. <p><button>运行</button></p>
  16. <div class="first"></div>
  17. <div class="second"></div>
  18.    
  19. <script>
  20.     $("button").click(function() {
  21.       $("div.first").slideUp(300).delay(800).fadeIn(400);
  22.       $("div.second").slideUp(300).fadeIn(400);
  23.     });
  24. </script>
  25.  
  26. </body>
  27. </html>

留意第一个绿色的层,在隐藏后,延迟了800毫秒才又触发fadeIn显示。

.detach():用于删除对象,同时保留删除对象数据。

detach这个方法非常有用,作用近似于.remove(),但比remove来的强大。detach在删除的同时,会返回被删除对象。
来看demo:http://www.36ria.com/demo/jquery1.4测试/detach.html
源代码如下:

  1. <html xmlns="http://www.w3.org/1999/xhtml">
  2. <head>
  3. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  4. <title>detach()</title>
  5. <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
  6.  
  7. <style>p { background:yellow; margin:6px 0; } p.off { background: black; }</style>
  8. </head>
  9.  
  10. <body>
  11.   <button>添加/删除段落</button>
  12.   <p>这是一个用于测试的段落</p> 
  13. <script>
  14.     var p;
  15.     $("button").click(function(){
  16.       if (p) {
  17.         p.appendTo("body");
  18.         p = null;
  19.       } else {
  20.         p = $("p").detach();
  21.       }
  22.     });
  23. </script>
  24.  
  25.  
  26. </body>
  27. </html>

留意p = $(“p”).detach();。删除了$(“p”)。但将$(“p”)写入了p 这个变量。

所有教程结束了,将把所有demo压缩下放出。

(如果您喜欢这篇教程,可以通过支付宝打赏我们1元哦,拜谢!)

跟作者说两句

:wink: :twisted: :roll: :oops: :mrgreen: :lol: :idea: :evil: :cry: :arrow: :?: :-| :-x :-o :-P :-? :) :( :!: 8-O 8)