将 jquery cookie 添加到内容切换

发布于 2024-12-04 18:51:56 字数 3150 浏览 0 评论 0原文

我使用以下代码来生成一个在单击时打开和关闭的内容区域,并带有一个加号/减号图像,该图像也会根据内容区域的状态进行切换。我想做的是添加一个 jquery cookie 来保存内容区域的状态 - 请问我该如何实现这一点? S

$(document).ready(function() {
                $("a.toggle").click(function() {
                    var img = $(this).find("img"); //get a handle of the image tag inside title link
                    var src = $(img).attr("src"); //get the source of the image                 
                    //toggle the source of the image to show either plus/minus
                    if (src.endsWith("search_open.jpg"))
                        src = src.replace('search_open.jpg', 'search_close.jpg');
                    else
                        src = src.replace('search_close.jpg', 'search_open.jpg');               
                    $(img).attr("src", src);                
                    //get the title of the anchor tag which corresponds to the id of the content div
                    var content = $(this).attr("title");
                    $(content).toggle();
                });                 
            });             
            String.prototype.endsWith = function(str) {
                return this.lastIndexOf(str) == this.length - str.length;
            }

编辑:最终代码/解决方案

$(document).ready(function() {                                         
                $("a.toggle").each(function () {
                    var img = $(this).find("img");
                    var src = $(img).attr("src");                        
                    var content = $(this).attr("title");
                    var isVisible = $.cookie('content');    
                    if (isVisible == 'true') {
                        $(content).show();
                        src = src.replace('search_open.jpg', 'search_close.jpg');
                    }
                    $(img).attr("src", src);    
                });                                                                          
                $("a.toggle").click(function() {
                    var img = $(this).find("img"); //get a handle of the image tag inside title link
                    var src = $(img).attr("src"); //get the source of the image                 
                    //toggle the source of the image to show either plus/minus
                    if (src.endsWith("search_open.jpg"))
                        src = src.replace('search_open.jpg', 'search_close.jpg');
                    else
                        src = src.replace('search_close.jpg', 'search_open.jpg');               
                    $(img).attr("src", src);                
                    //get the title of the anchor tag which corresponds to the id of the content div
                    var content = $(this).attr("title");
                    $(content).toggle();
                    $.cookie('content', $(content).is(':visible'));                     
                });                     
            });             
            String.prototype.endsWith = function(str) {
                return this.lastIndexOf(str) == this.length - str.length;
            }   

I'm using the following code to to produce a content area that opens and closes on click, with a plus/minus image that also toggles depending on state of content area. What I would like to do is add a jquery cookie that saves the state of the content area - how would I achieve this please? S

$(document).ready(function() {
                $("a.toggle").click(function() {
                    var img = $(this).find("img"); //get a handle of the image tag inside title link
                    var src = $(img).attr("src"); //get the source of the image                 
                    //toggle the source of the image to show either plus/minus
                    if (src.endsWith("search_open.jpg"))
                        src = src.replace('search_open.jpg', 'search_close.jpg');
                    else
                        src = src.replace('search_close.jpg', 'search_open.jpg');               
                    $(img).attr("src", src);                
                    //get the title of the anchor tag which corresponds to the id of the content div
                    var content = $(this).attr("title");
                    $(content).toggle();
                });                 
            });             
            String.prototype.endsWith = function(str) {
                return this.lastIndexOf(str) == this.length - str.length;
            }

Edit: final code/solution

$(document).ready(function() {                                         
                $("a.toggle").each(function () {
                    var img = $(this).find("img");
                    var src = $(img).attr("src");                        
                    var content = $(this).attr("title");
                    var isVisible = $.cookie('content');    
                    if (isVisible == 'true') {
                        $(content).show();
                        src = src.replace('search_open.jpg', 'search_close.jpg');
                    }
                    $(img).attr("src", src);    
                });                                                                          
                $("a.toggle").click(function() {
                    var img = $(this).find("img"); //get a handle of the image tag inside title link
                    var src = $(img).attr("src"); //get the source of the image                 
                    //toggle the source of the image to show either plus/minus
                    if (src.endsWith("search_open.jpg"))
                        src = src.replace('search_open.jpg', 'search_close.jpg');
                    else
                        src = src.replace('search_close.jpg', 'search_open.jpg');               
                    $(img).attr("src", src);                
                    //get the title of the anchor tag which corresponds to the id of the content div
                    var content = $(this).attr("title");
                    $(content).toggle();
                    $.cookie('content', $(content).is(':visible'));                     
                });                     
            });             
            String.prototype.endsWith = function(str) {
                return this.lastIndexOf(str) == this.length - str.length;
            }   

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

太阳哥哥 2024-12-11 18:51:56

使用 jQuery.cookie 插件来处理 cookie。使用它存储和获取必要的数据。

https://github.com/carhartl/jquery-cookie

查看项目中的测试示例:

< a href="https://github.com/carhartl/jquery-cookie/blob/master/test.js" rel="nofollow">https://github.com/carhartl/jquery-cookie/blob/master/ test.js

$.cookie('c') // get cookie value
$.cookie('c', 'my value') // set cookie value

你将需要为您切换的每个部分存储一些标记(可能是一些自动增量 ID),然后在 $(document).ready(..) 上您将需要读取并恢复这些部分的状态。

$(document).ready(function () {

    $("a.toggle").each(function () {
        var content = $(this).attr("title");
        var isVisible = $.cookie(content);
        if (isVisible) {
            $(content).show();
        }
    });

    $("a.toggle").click(function () {
        var img = $(this).find("img"); //get a handle of the image tag inside title link
        var src = $(img).attr("src"); //get the source of the image                 
        //toggle the source of the image to show either plus/minus
        if (src.endsWith("search_open.jpg"))
            src = src.replace('search_open.jpg', 'search_close.jpg');
        else
            src = src.replace('search_close.jpg', 'search_open.jpg');
        $(img).attr("src", src);
        //get the title of the anchor tag which corresponds to the id of the content div
        var content = $(this).attr("title");
        $(content).toggle();
        $.cookie(content, $(content).is(':visible'));
    });
});

String.prototype.endsWith = function (str) {
    return this.lastIndexOf(str) == this.length - str.length;
}

Use jQuery.cookie plugin to work with cookies. Store and get necessary data using it.

https://github.com/carhartl/jquery-cookie

Checkout test examples in the project:

https://github.com/carhartl/jquery-cookie/blob/master/test.js

$.cookie('c') // get cookie value
$.cookie('c', 'my value') // set cookie value

You will need to strore some marker (probably some autoincremental id) for each section you toggled, and then on $(document).ready(..) you will need to read and resotre state of these sections.

$(document).ready(function () {

    $("a.toggle").each(function () {
        var content = $(this).attr("title");
        var isVisible = $.cookie(content);
        if (isVisible) {
            $(content).show();
        }
    });

    $("a.toggle").click(function () {
        var img = $(this).find("img"); //get a handle of the image tag inside title link
        var src = $(img).attr("src"); //get the source of the image                 
        //toggle the source of the image to show either plus/minus
        if (src.endsWith("search_open.jpg"))
            src = src.replace('search_open.jpg', 'search_close.jpg');
        else
            src = src.replace('search_close.jpg', 'search_open.jpg');
        $(img).attr("src", src);
        //get the title of the anchor tag which corresponds to the id of the content div
        var content = $(this).attr("title");
        $(content).toggle();
        $.cookie(content, $(content).is(':visible'));
    });
});

String.prototype.endsWith = function (str) {
    return this.lastIndexOf(str) == this.length - str.length;
}
别再吹冷风 2024-12-11 18:51:56

是的,您可以使用 jQuery.cookie。

            $("a.toggle").click(function() {
                var img = $(this).find("img"); //get a handle of the image tag inside title link
                var src = $(img).attr("src"); //get the source of the image                 
                //toggle the source of the image to show either plus/minus
                if (src.endsWith("search_open.jpg"))
                    src = src.replace('search_open.jpg', 'search_close.jpg');
                else
                    src = src.replace('search_close.jpg', 'search_open.jpg');               
                $(img).attr("src", src);                
                //get the title of the anchor tag which corresponds to the id of the content div
                var content = $(this).attr("title");
                $(content).toggle();
                //this saves a string with true or false depending on the visibility
                //of your element. i use the title of the element as name for the cookie so that you can save more than one
                $.cookie(content, $(content).is(':visible') );
            });                 
        });  

Yes you could use jQuery.cookie.

            $("a.toggle").click(function() {
                var img = $(this).find("img"); //get a handle of the image tag inside title link
                var src = $(img).attr("src"); //get the source of the image                 
                //toggle the source of the image to show either plus/minus
                if (src.endsWith("search_open.jpg"))
                    src = src.replace('search_open.jpg', 'search_close.jpg');
                else
                    src = src.replace('search_close.jpg', 'search_open.jpg');               
                $(img).attr("src", src);                
                //get the title of the anchor tag which corresponds to the id of the content div
                var content = $(this).attr("title");
                $(content).toggle();
                //this saves a string with true or false depending on the visibility
                //of your element. i use the title of the element as name for the cookie so that you can save more than one
                $.cookie(content, $(content).is(':visible') );
            });                 
        });  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文