<title>data()函数-软件开发网</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”>把数据添加元素</button>
<button id=”show”>获取添加到元素的数据</button>
</body>
</html>
以上代码能够以键/值对方式为div附加名称为mydata的数据,然后通过数据名和键取得附加的数据值。
语法结构四:
使用对象方式为指定元素附加数据。
$(selector).data(object)
参数列表:
实例代码:
<!DOCTYPE html>
<html>
<head>
<meta charset=” utf-8″>
<meta name=”author” content=”//www.jb51.net/” />
<title>data()函数-软件开发网</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专区”;
$(“#add”).click(function(){
$(“div”).data(mytest);
})
$(“#show”).click(function(){
alert($(“div”).data(“second”));
})
});
</script>
</head>
<body>
<div></div>
<button id=”add”>把数据添加元素</button>










