国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > JavaScript > 正文

jQuery中data()方法用法實(shí)例

2019-11-20 13:35:33
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

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

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

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

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

參數(shù)列表:

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

實(shí)例代碼:

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

<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<meta name="author" content="http://m.survivalescaperooms.com/" />
<title>data()函數(shù)-武林網(wǎng)</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","武林網(wǎng)歡迎您");
  })
  $("#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,值為“武林網(wǎng)歡迎您”的數(shù)據(jù),然后利用數(shù)據(jù)名稱返回。

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

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

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

參數(shù)列表:

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

實(shí)例代碼:

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

<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<meta name="author" content="http://m.survivalescaperooms.com/" />
<title>data()函數(shù)-武林網(wǎng)</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","武林網(wǎng)歡迎您");
  })
  $("#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,值為“武林網(wǎng)歡迎您”的數(shù)據(jù),然后利用數(shù)據(jù)名稱返回。

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

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

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

參數(shù)列表:

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

實(shí)例代碼:

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

<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<meta name="author" content="http://m.survivalescaperooms.com/" />
<title>data()函數(shù)-武林網(wǎng)</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>

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

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

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

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

參數(shù)列表:

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

實(shí)例代碼:

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

<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<meta name="author" content="http://m.survivalescaperooms.com/" />
<title>data()函數(shù)-武林網(wǎng)</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="武林網(wǎng)歡迎您";
  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>

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

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

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 龙门县| 海南省| 德格县| 蓬溪县| 久治县| 林芝县| 马边| 东山县| 桐梓县| 鸡泽县| 景德镇市| 杂多县| 安仁县| 宁远县| 琼中| 沂源县| 古交市| 赣州市| 上蔡县| 镶黄旗| 旅游| 陆川县| 沁阳市| 华阴市| 乐平市| 青神县| 麦盖提县| 湘乡市| 江油市| 深泽县| 五寨县| 屏边| 灌南县| 恩平市| 自治县| 定安县| 隆德县| 绥芬河市| 雅江县| 镶黄旗| 长白|