<!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>
?