抱歉,标题不好。为了我的生命,我想不出一个好办法。我有一个jquery对象,用于设置cookie。在我的代码中,我使用该cookie从用户那里收集信息,并执行各种其他功能。问题是,在某些情况下,我不希望cookie名称是'user-profile',我希望它是'admin-profile'.,在调用userProfile()并更改cookie名称时,有什么方法可以传递选项吗?我希望'admin-profile' cookie基本上可以完成'user-profile' cookie所做的一切。我是个新手,所以代码样本最适合我。
;(function($){
$.fn.userProfile = function(opts) {
var userProfileCookie = {
name: 'user-profile',
options: {
path: '/',
expires: 365
}
};
function userHeightCookie() {
var userData = $.parseJSON($.cookie(userProfileCookie.name));
return(userData);
};
function readHeightCookie(userInfo) {
$.cookie(userProfileCookie.name, JSON.stringify(userInfo), userProfileCookie.options);
};
function removeProfileCookie() { $.cookie(userProfileCookie.name, null, userProfileCookie.options); }
if($(".slider").length > 0){
$.cookie(userProfileCookie.name);
}
}})(jQuery);
$(document).ready(function () { $('#mastHead').userProfile(); });发布于 2014-01-27 20:57:24
为此使用opts参数:
;(function($){
$.fn.userProfile = function(opts) {
var name = (opts.name || 'user') + '-profile';
var userProfileCookie = {
name: name,
options: {
path: '/',
expires: 365
}
};
function userHeightCookie() {
var userData = $.parseJSON($.cookie(userProfileCookie.name));
return(userData);
};
function readHeightCookie(userInfo) {
$.cookie(userProfileCookie.name, JSON.stringify(userInfo), userProfileCookie.options);
};
function removeProfileCookie() { $.cookie(userProfileCookie.name, null, userProfileCookie.options); }
if($(".slider").length > 0){
$.cookie(userProfileCookie.name);
}
}})(jQuery);
$(document).ready(function () { $('#mastHead').userProfile({ name: 'admin' }); });https://stackoverflow.com/questions/21391391
复制相似问题