886.jquery插件cookies的使用
先上下代码:
<html> <head> <script type="text/javascript" src="jquery.js"></script> //插入js <script type="text/javascript" src="jquery.cookies.2.2.0.min.js"></script>//插入插件。注意:这个插件一定要放在jquery文件的后面,否则不会生效 <script type="text/javascript"> $(document).ready(function(){ $(".tijiao").click(function(){ //触发button事件 $.cookies.set('name',$(".userName").value); $.cookies.set('password',$(".password").value); //把表单中得到的value存入cookie alert("已经存入cookie"); }); var userN=$.cookies.get( 'name' ); var userP=$.cookies.get( 'password' ); //从cookie中读取数据 if(userN!=""&&userN!=null&&userP!=""&&userP!=null) {$(".userName").value=userN; $(".password").value=userP; // 赋值 } else{ alert("userName or password is not exist");}; }) </script> </head> <body> <div> <table> <tr id="name"> <td><lable>用户名:</lable></td> <td><input type="text" class="userName" value=""/></td> </tr> <tr> <td><lable>密码:</lable></td> <td><input type="password1" class="password" value=""/></td> </tr> <tr> <td><input type="button" class="tijiao" value="登录"/></td> </tr> </table> </div> </body> </html>
要想设置cookie的其他属性就看这个:
var newOptions = { domain: '*.mydomain.com',//设置域 path: '/somedir',//设置路径 expiresAt: new Date( 2011, 1, 1 ),//设置cookie失效时间 hoursToLive:0.01//设置cookie存活时间 secure: true//是否保护 }
jquery插件cookies的下载地址:
http://code.google.com/p/cookies/downloads/list
相关的文档: