本文實(shí)例講述了jQuery中data()方法用法。分享給大家供大家參考。具體分析如下:
此方法可以向匹配元素附加數(shù)據(jù),或者從匹配元素獲取數(shù)據(jù)。
語(yǔ)法結(jié)構(gòu)一:
$(selector).data(name,value)
參數(shù)列表:
| 參數(shù) | 描述 |
| name | 存儲(chǔ)的數(shù)據(jù)名稱。 |
| value | 將要存儲(chǔ)的任意數(shù)據(jù)。 |
實(shí)例代碼:
<!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ù)。
$(selector).data(name)
參數(shù)列表:
| 參數(shù) | 描述 |
| name | 存儲(chǔ)的數(shù)據(jù)名稱。 |
實(shí)例代碼:
<!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ù)。
$(selector).data(properties)
參數(shù)列表:
| 參數(shù) | 描述 |
| properties | 一個(gè)用于設(shè)置數(shù)據(jù)的鍵/值對(duì)。 |
實(shí)例代碼:
<!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ù)。
$(selector).data(object)
參數(shù)列表:
| 參數(shù) | 描述 |
| object | 一個(gè)用于設(shè)置數(shù)據(jù)的對(duì)象。 |
實(shí)例代碼:
<!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ì)有所幫助。