首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >jquery -通过参数更改jquery对象

jquery -通过参数更改jquery对象
EN

Stack Overflow用户
提问于 2014-01-27 20:50:53
回答 1查看 32关注 0票数 0

抱歉,标题不好。为了我的生命,我想不出一个好办法。我有一个jquery对象,用于设置cookie。在我的代码中,我使用该cookie从用户那里收集信息,并执行各种其他功能。问题是,在某些情况下,我不希望cookie名称是'user-profile',我希望它是'admin-profile'.,在调用userProfile()并更改cookie名称时,有什么方法可以传递选项吗?我希望'admin-profile' cookie基本上可以完成'user-profile' cookie所做的一切。我是个新手,所以代码样本最适合我。

代码语言:javascript
复制
;(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(); });
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-01-27 20:57:24

为此使用opts参数:

代码语言:javascript
复制
;(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' }); });
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21391391

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档