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

首頁(yè) > 語(yǔ)言 > JavaScript > 正文

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

2024-05-06 16:13:01
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
這篇文章主要介紹了jQuery中data()方法用法,實(shí)例分析了data()方法向匹配元素附加數(shù)據(jù)及獲取數(shù)據(jù)的使用技巧,具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
 
 

本文實(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ā)表

圖片精選

主站蜘蛛池模板: 周口市| 运城市| 鄂州市| 临猗县| 沁阳市| 拜泉县| 肃北| 灵石县| 昌黎县| 射洪县| 襄城县| 株洲县| 正安县| 正宁县| 松阳县| 老河口市| 明星| 温宿县| 合江县| 永兴县| 正安县| 二连浩特市| 崇州市| 黄石市| 洪洞县| 成都市| 天镇县| 玉林市| 古交市| 林口县| 新野县| 龙江县| 诏安县| 巢湖市| 广水市| 闵行区| 满洲里市| 北辰区| 正蓝旗| 巴青县| 黑龙江省|