发布于 2016-09-11 20:57:39 | 182 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的jQuery示例,程序狗速度看过来!

jQuery javascript框架

jQuery是一个兼容多浏览器的javascript框架,核心理念是write less,do more(写得更少,做得更多)。jQuery在2006年1月由美国人John Resig在纽约的barcamp发布,吸引了来自世界各地的众多JavaScript高手加入,由Dave Methvin率领团队进行开发。


本文将介绍jQuery如何操作Cookie插件,需要了解的朋友可以参考下
代码:
 
jQuery.cookie = function(name, value, options) { 
if (typeof value != 'undefined') { // name and value given, set cookie 
options = options || {}; 
if (value === null) { 
value = ''; 
options = $.extend({}, options); // clone object since it's unexpected behavior if the expired property were changed 
options.expires = -1; 
} 
var expires = ''; 
if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) { 
var date; 
if (typeof options.expires == 'number') { 
date = new Date(); 
date.setTime(date.getTime() (options.expires * 24 * 60 * 60 * 1000)); 
} else { 
date = options.expires; 
} 
expires = '; expires=' date.toUTCString(); // use expires attribute, max-age is not supported by IE 
} 
// NOTE Needed to parenthesize options.path and options.domain 
// in the following expressions, otherwise they evaluate to undefined 
// in the packed version for some reason... 
var path = options.path ? '; path=' (options.path) : ''; 
var domain = options.domain ? '; domain=' (options.domain) : ''; 
var secure = options.secure ? '; secure' : ''; 
document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join(''); 
} else { // only name given, get cookie 
var cookieValue = null; 
if (document.cookie && document.cookie != '') { 
var cookies = document.cookie.split(';'); 
for (var i = 0; i < cookies.length; i ) { 
var cookie = jQuery.trim(cookies[i]); 
// Does this cookie string begin with the name we want? 
if (cookie.substring(0, name.length 1) == (name '=')) { 
cookieValue = decodeURIComponent(cookie.substring(name.length 1)); 
break; 
} 
} 
} 
return cookieValue; 
} 
}; 

使用方法
jQuery操作cookie的插件,大概的使用方法如下
$.cookie('the_cookie'); //读取Cookie值
$.cookie('the_cookie', 'the_value'); //设置cookie的值
$.cookie('the_cookie', 'the_value', {expires: 7, path: '/', domain: 'jquery.com', secure: true});//新建一个cookie 包括有效期 路径 域名等
$.cookie('the_cookie', 'the_value'); //新建cookie
$.cookie('the_cookie', null); //删除一个cookie
设置一个名称为blog,值为css9.net的cookie:
$.cookie("blog", "css9.net");
设置一个名称为blog,值为css9.net的cookie,同时设置过期时间(expires属性)为7天:
$.cookie("blog", "css9.net", { expires: 7 });
设置一个名称为blog,值为css9.net的cookie,设置过期时间(expires属性)为7天,同时设置cookie的path属性为”/admin”
$.cookie("blog", "css9.net", { path: '/admin', expires: 7 });
读取Cookie:
读取名称为blog的cookie值:
alert( $.cookie("blog") );
删除cookie:
$.cookie("example", null);

最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务