在HTML5 中,新增加了data-attributes属性,可以在页面级保存一些数据,比如:
<body>
<div data-site="www.codeguru.com" data-category="Technology">Hi, Welcome to HTML 5 data attributes!</div>
<div data-name="Dave" data-age=23 data-dept="Clerical">Hi Dave!</div>
</body>
</html>
而在jquery 中,可以通过.data的方法去获得和设置data attributes,比如:
<!DOCTYPE html>
<html>
<head>
<title>Untitled Page</title>5.
<script type="text/javascript">
$("#btnSet").click(function(e) {
$("#divEmployee").data("name", "Dave");
$("#divEmployee").data("age", "23");
$("#divEmployee").data("dept", "Clerical");
10.
});</script>
</head>
<body>
<div id="divEmployee">
Hi Dave!
</div>
15.
<input type="button" id="btnSet" value="Set Value" />
</body>
</html>
可以看到设置了divEmployee的一系列属性,包括name,age,dept.
也可以用如下两个方法设置:
<script type="text/javascript">
$("#btnSet").click(function(e) {
$("#divEmployee").data( {
"name" : "Dave",
"age" : 23,
"dept" : "Clerical"
});
});
</script>
<script type="text/javascript">
$("#btnSet").click(function(e) {
$("#divEmployee").data("name", "age", "dept").data("Dave", 23, "Clerical");
});
</script>
而获取和删除的方法分别如下:
<script type="text/javascript">
$("#btnSet").click(function(e) {
var name = $("#divEmployee").data("name")
var age = $("#divEmployee").data("age")
var dept = $("#divEmployee").data("dept")
});
</script>
<script type="text/javascript">
$("#btnSet").click(function(e) {
$("#divEmployee").removeData("employee");
});</script>
分享到:
相关推荐
基于jQuery的HTML5移动端音乐播放器源码 基于jQuery的HTML5移动端音乐播放器源码 基于jQuery的HTML5移动端音乐播放器源码 基于jQuery的HTML5移动端音乐播放器源码 基于jQuery的HTML5移动端音乐播放器...
editplus html5 css3 JQuery 语法文件 html5 css3.0 jQueryAPI手册
《HTML5+CSS3+jQuery应用之美》案例完整,跟学书中实例后,相信会使读者的网页设计与制作水平均有实质提升。, 《HTML5+CSS3+jQuery应用之美》适合Web设计人员和前端开发人员阅读,也适合作为相关培训用途的参考用书...
Jquery学习 Jquery源代码 Jquery数据库操作 Jquery学习 Jquery源代码 Jquery数据库操作 绝对有用,技术含量
HTML5 PHP jquery uploadify上传文件,带进度条,author:吕大豹。仿照uploadify写的,www.codesc.net已做过修正,本例的配置参数均与uploadify官网一致,参照官网的api就可以了,直接把文件夹上传到支持php的服务器...
jquery中data的案例,获取同一标签内的数据,以及移除标签内的数据
Jquery各种连接操作 Ajax JqueryJquery各种连接操作 Ajax Jquery
HTML5 JQuery POC HTML5 JQuery POC
jQuery操作文件夹
前端web开发,情人节浪漫专属,表白源码。利用jquery+html5实现
jquerymobile是用于创建移动 Web 应用的前端开发框架,此资源包含了几套jquerymobile的模板,供新手学习。
jquery表格操作实例jquery表格操作实例jquery表格操作实例jquery表格操作实例
用html5+css3和jquery制作比较好网页模板
列举多种jquery html5常见的多种导航滑动效果
Uploadify html5 官网下载 竟然要收费,太可气了,这个是根据官网API做的和uploadify一样基于html5 的上传插件。部署很简单。
使用jquery-mobile+html5实现android本地通讯录
jQuery第4天知识点:jQuery节点操作、jQuery特殊属性操作、jQuery事件机制.zip
JQuery DoM和ajax 操作大全源码
jQuery+html5视频与音频播放器
基于JQuery和html5的拼图游戏类库和实例