jQuery日本語リファレンス

jQuery does not mean Japanese Query...

Internals/API/jQuery - Sample Code

jQuery.data(elem, name)


            
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
                    "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
  <script src="/jquery/js/jquery.js"></script>
  
  <script>
  $(document).ready(function(){
    
    $("button").click(function(e) {
      var adiv = $("#adiv").get(0);
      var value;

      switch ($("button").index(this)) {
        case 0 :
          jQuery.data(adiv, "blah", "hello");
          break;
        case 1 :
          jQuery.data(adiv, "blah", 86);
          break;
        case 2 :
          jQuery.removeData(adiv);
          break;
      }
      value = jQuery.data(adiv, "blah");

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

  });
  </script>
  <style>
  div { margin:5px; background:yellow; }
  button { margin:5px; font-size:14px; }
  p { margin:5px; color:blue; }
  span { color:red; }
  </style>
</head>
<body>
  <div id="adiv">DIV要素</div>
  <button>"blah" に "hello" を代入</button>
  <button>"blah" に 86 を代入</button>
  <button>"blah" の値を削除</button>
  <p>DIV要素の "blah" の現在値は <span>?</span> です。</p>
</body>
</html>
            
          
[実行結果]
[jQuery.data(elem, name) の説明へ]