jquery 数据缓存 data方法使用
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <TITLE>data方法带函数</TITLE> <script type="text/javascript" src="../jquery-1.4.1.js"></script> <script type="text/javascript"> function getDivData(){ var t = document.getElementById("testId"); //取缓存数据 var state = $.data(t,'test'); var fn = state.hanshu ; //执行函数 fn(); } function getDivData1(){ var t = document.getElementById("testId"); //取缓存数据 var state = $.data(t,'test1'); var fn = state.hanshu ; alert(fn); } //改变一个数据 function changeDivData(){ alert(111111111111); return "函数返回值"; } $(function(){ var t = document.getElementById("testId"); //此处必须用document.getElementById("testId")用$("#testId")没有效果 $.data(t,'test',{ name:'hqm', age:27, hanshu:changeDivData //此处hanshu为一个函数 }); $.data(t,'test1',{ name:'hqm', age:27, hanshu:changeDivData()//此处hanshu为值函数返回值,因为执行了该函数 }); }); </script> </HEAD> <BODY> <div id="testId">在这个div上存些数据</div> <input type="button" value="getDivData" onclick="getDivData()"/> <input type="button" value="getDivData1" onclick="getDivData1()"/> </BODY> </HTML>
相关推荐
jianxm 2020-06-13
luofuIT成长记录 2020-03-27
changjiang 2017-08-03
yFifhting 2015-04-13
PHP基础学习 2017-03-06
skyGAYD 2017-04-09
小小天下飞 2019-08-23
kobeyan 2012-11-22
hhhkhhh 2017-04-09
honeyth 2017-03-06
麦盟Maimob 2016-11-23
Balalala 2015-04-13
skyGAYD 2015-03-28
何双江yarshray 2015-01-26
Jiutocrx 2014-08-05
xiaohuli 2013-07-09