Jquery核心(三) Data

jQuery數據

[1] data( name )      Returns: Any
      name :  String 存儲的數據的標識。
      說明:  返回data(name, value)設置的名爲name的數據。如果一個jQuery集合中有多個元素,則返回第一個元素的值。該方法用於避免循環引用的風險地獲取存儲的數據。
     

Js代碼 複製代碼
  1. $("button").click(function(e) {   
  2.       var value;   
  3.   
  4.       switch ($("button").index(this)) {   
  5.         case 0 :   
  6.           value = $("div").data("blah");   
  7.           break;   
  8.         case 1 :   
  9.           $("div").data("blah""hello");   
  10.           value = "Stored!";   
  11.           break;   
  12.         case 2 :   
  13.           $("div").data("blah", 86);   
  14.           value = "Stored!";   
  15.           break;   
  16.         case 3 :   
  17.           $("div").removeData("blah");   
  18.           value = "Removed!";   
  19.           break;   
  20.       }   
  21.   
  22.       $("span").text("" + value);   
  23.     });  
$("button").click(function(e) {
      var value;

      switch ($("button").index(this)) {
        case 0 :
          value = $("div").data("blah");
          break;
        case 1 :
          $("div").data("blah", "hello");
          value = "Stored!";
          break;
        case 2 :
          $("div").data("blah", 86);
          value = "Stored!";
          break;
        case 3 :
          $("div").removeData("blah");
          value = "Removed!";
          break;
      }

      $("span").text("" + value);
    });




[2] data( name, value )      Returns: jQuery
      name :  String 存儲的數據的標識。
      value:   Any 存儲的數據。 
      說明:  設置數據。
     

Js代碼 複製代碼
  1. $("div").data("test", { first: 16, last: "pizza!" });   
  2.    $("span:first").text($("div").data("test").first);   
  3.    $("span:last").text($("div").data("test").last);  
 $("div").data("test", { first: 16, last: "pizza!" });
    $("span:first").text($("div").data("test").first);
    $("span:last").text($("div").data("test").last);




[3] removeData( name )       Returns: jQuery
      name :  String 存儲的數據的標識。
      說明:  移除數據。
     

Js代碼 複製代碼
  1. $("div").data("test1""VALUE-1");   
  2. $("div").removeData("test1");  
 $("div").data("test1", "VALUE-1");
 $("div").removeData("test1");



[4] queue( [name] )        Returns: Array<Function>
      name :  String 隊列的標識(默認爲fx)。
      說明:  返回第一個匹配元素的隊列(爲一個函數數組an array of functions)。
     

Js代碼 複製代碼
  1. $("#show").click(function () {   
  2.     var n = $("div").queue("fx");   
  3.     $("span").text("Queue length is: " + n.length);   
  4.   });   
  5.   function runIt() {   
  6.     $("div").show("slow");   
  7.     $("div").animate({left:'+=200'},2000);   
  8.     $("div").slideToggle(1000);   
  9.     $("div").slideToggle("fast");   
  10.     $("div").animate({left:'-=200'},1500);   
  11.     $("div").hide("slow");   
  12.     $("div").show(1200);   
  13.     $("div").slideUp("normal", runIt);   
  14.   }   
  15.   runIt();  
  $("#show").click(function () {
      var n = $("div").queue("fx");
      $("span").text("Queue length is: " + n.length);
    });
    function runIt() {
      $("div").show("slow");
      $("div").animate({left:'+=200'},2000);
      $("div").slideToggle(1000);
      $("div").slideToggle("fast");
      $("div").animate({left:'-=200'},1500);
      $("div").hide("slow");
      $("div").show(1200);
      $("div").slideUp("normal", runIt);
    }
    runIt();



[5] queue( [name], callback )        Returns: jQuery
      name :  String 存儲的數據的標識(默認爲fx)。
      callback :  Function 增加到隊列的函數。
      說明:  在所有匹配元素的隊列尾部添加一個函數並立即執行。
     

Js代碼 複製代碼
  1. $(document.body).click(function () {   
  2.      $("div").show("slow");   
  3.      $("div").animate({left:'+=200'},2000);   
  4.      $("div").queue(function () {   
  5.        $(this).addClass("newcolor");   
  6.        $(this).dequeue();   
  7.      });   
  8.      $("div").animate({left:'-=200'},500);   
  9.      $("div").queue(function () {   
  10.        $(this).removeClass("newcolor");   
  11.        $(this).dequeue();   
  12.      });   
  13.      $("div").slideUp();   
  14.    });  
 $(document.body).click(function () {
      $("div").show("slow");
      $("div").animate({left:'+=200'},2000);
      $("div").queue(function () {
        $(this).addClass("newcolor");
        $(this).dequeue();
      });
      $("div").animate({left:'-=200'},500);
      $("div").queue(function () {
        $(this).removeClass("newcolor");
        $(this).dequeue();
      });
      $("div").slideUp();
    });



[6] queue( [name], queue )        Returns: jQuery
      name :  String 存儲的數據的標識(默認爲fx)。
      queue :  Array<Function>替換原隊列的隊列。
      說明:  用一個新隊列替換匹配的元素的隊列。


[7] dequeue( [name] )        Returns: jQuery
      name :  String 存儲的數據的標識(默認爲fx)。
      說明:  將隊首函數出隊,並執行。

發佈了38 篇原創文章 · 獲贊 0 · 訪問量 1358
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章