亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

jQuery中data()方法用法實例

 更新時間:2014年12月27日 14:51:55   投稿:shichen2014  
這篇文章主要介紹了jQuery中data()方法用法,實例分析了data()方法向匹配元素附加數(shù)據(jù)及獲取數(shù)據(jù)的使用技巧,具有一定的參考借鑒價值,需要的朋友可以參考下

本文實例講述了jQuery中data()方法用法。分享給大家供大家參考。具體分析如下:

此方法可以向匹配元素附加數(shù)據(jù),或者從匹配元素獲取數(shù)據(jù)。

語法結(jié)構(gòu)一:

復(fù)制代碼 代碼如下:
$(selector).data(name,value)

參數(shù)列表:

參數(shù) 描述
name 存儲的數(shù)據(jù)名稱。
value 將要存儲的任意數(shù)據(jù)。

實例代碼:

復(fù)制代碼 代碼如下:

<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<meta name="author" content="http://chabaoo.cn/" />
<title>data()函數(shù)-腳本之家</title>
<script type="text/javascript" src="mytest/jQuery/jquery-1.8.3.js"></script>
<script type="text/javascript">
$(document).ready(function(){
  $("#add").click(function(){
    $("div").data("mydata","腳本之家歡迎您");
  })
  $("#show").click(function(){
    $("div").text($("div").data("mydata"));
  })
})
</script>
</head>
<body>
<div></div>
<button id="add">向元素添加數(shù)據(jù)</button>
<button id="show">顯示添加的數(shù)據(jù)</button>
</body>
</html>

以上代碼能夠在匹配的div元素上附加名稱mydata,值為“腳本之家歡迎您”的數(shù)據(jù),然后利用數(shù)據(jù)名稱返回。

語法結(jié)構(gòu)二:

從匹配元素中返回指定數(shù)據(jù)名稱的附加的數(shù)據(jù)。

復(fù)制代碼 代碼如下:
$(selector).data(name)

參數(shù)列表:

參數(shù) 描述
name 存儲的數(shù)據(jù)名稱。

實例代碼:

復(fù)制代碼 代碼如下:

<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<meta name="author" content="http://chabaoo.cn/" />
<title>data()函數(shù)-腳本之家</title>
<script type="text/javascript" src="mytest/jQuery/jquery-1.8.3.js"></script>
<script type="text/javascript">
$(document).ready(function(){
  $("#add").click(function(){
    $("div").data("mydata","腳本之家歡迎您");
  })
  $("#show").click(function(){
    $("div").text($("div").data("mydata"));
  })
})
</script>
</head>
<body>
  <div></div>
  <button id="add">向元素添加數(shù)據(jù)</button>
  <button id="show">顯示添加的數(shù)據(jù)</button>
</body>
</html>

以上代碼能夠在匹配的div元素上附加名稱mydata,值為“腳本之家歡迎您”的數(shù)據(jù),然后利用數(shù)據(jù)名稱返回。

語法結(jié)構(gòu)三:

使用鍵/值對對象向匹配元素添加數(shù)據(jù)。

復(fù)制代碼 代碼如下:
$(selector).data(properties)

參數(shù)列表:

參數(shù) 描述
properties 一個用于設(shè)置數(shù)據(jù)的鍵/值對。

實例代碼:

復(fù)制代碼 代碼如下:

<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<meta name="author" content="http://chabaoo.cn/" />
<title>data()函數(shù)-腳本之家</title>
<script type="text/javascript" src="mytest/jQuery/jquery-1.8.3.js"></script>
<script type="text/javascript">
$(document).ready(function(){
  $("#add").click(function(){
    $("div").data("mydata",{username:"daoliang"});
  })
  $("#show").click(function(){
    alert($("div").data("mydata").username);
  })
})
</script>
</head>
<body>
<div></div>
<button id="add">把數(shù)據(jù)添加元素</button>
<button id="show">獲取添加到元素的數(shù)據(jù)</button>
</body>
</html>

以上代碼能夠以鍵/值對方式為div附加名稱為mydata的數(shù)據(jù),然后通過數(shù)據(jù)名和鍵取得附加的數(shù)據(jù)值。

語法結(jié)構(gòu)四:

使用對象方式為指定元素附加數(shù)據(jù)。

復(fù)制代碼 代碼如下:
$(selector).data(object)

參數(shù)列表:

參數(shù) 描述
object 一個用于設(shè)置數(shù)據(jù)的對象。

實例代碼:

復(fù)制代碼 代碼如下:

<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<meta name="author" content="http://chabaoo.cn/" />
<title>data()函數(shù)-腳本之家</title>
<script type="text/javascript" src="mytest/jQuery/jquery-1.8.3.js"></script>
<script type="text/javascript">
$(document).ready(function(){
  var mytest=new Object();
  mytest.first="腳本之家歡迎您";
  mytest.second="JQuery專區(qū)";
  $("#add").click(function(){
    $("div").data(mytest);
  })
  $("#show").click(function(){
    alert($("div").data("second"));
  })
});
</script>
</head>
<body>
  <div></div>
  <button id="add">把數(shù)據(jù)添加元素</button>
  <button id="show">獲取添加到元素的數(shù)據(jù)</button>
</body>
</html>

以上代碼以對象方式附加數(shù)據(jù),并且取得附加的數(shù)據(jù)值。

希望本文所述對大家的jQuery程序設(shè)計有所幫助。

相關(guān)文章

最新評論