如何使用cookie中存储的值打开元素

时间:2011-07-14 07:22:25

标签: jquery

如何使用cookie中的存储值打开元素。我能够从第一页中单击的元素中检索cookie值,但是我如何使用cookie值来显示页面中的元素?

第一页:

<script>
    $(document).ready(function(){
        //jquery toggle menu
        $(".product-contents").hide();
        $("div.product-header").click(function(){
            $(this).toggleClass("active").next().slideToggle("fast");
        });

        //setting the clicked element value in the cookie
        $("#product-list div").click(function() {
            var index = $("#product-list div").index(this);
            $.cookie("product_name", index);
        });
    });
 </script>

切换菜单

<div id="product-list">
<div class="product-wrapper">
    <div class="product-header">sampletest</div>
    <div class="product-contents" style="display: none;">
    <div class="product-subheader"><a href="/printing/sampletest/18">flat black matte</a></div>
    <div class="product-subheader"><a href="/printing/sampletest/19">flat black matte</a></div>
    <div class="product-subheader"><a href="/printing/sampletest/30">product two</a></div>
    <div class="product-subheader"><a href="/printing/sampletest/24">In augue tellus</a></div>
</div>
<div class="product-wrapper">
    <div class="product-header">sampletest</div>
    <div class="product-contents" style="display: none;">
    <div class="product-subheader"><a href="/printing/sampletest/33">flat black matte1</a></div>
    <div class="product-subheader"><a href="/printing/sampletest/34">flat black matte3</a></div>
    <div class="product-subheader"><a href="/printing/sampletest/35">product two4</a></div>
    <div class="product-subheader"><a href="/printing/sampletest/36">In augue tellus55</a></div>
</div>
<div class="product-wrapper">
    <div class="product-header">sampletest</div>
    <div class="product-contents" style="display: none;">
    <div class="product-subheader"><a href="/printing/sampletest/66">flat black matte3</a></div>
    <div class="product-subheader"><a href="/printing/sampletest/77">flat black matte3</a></div>
    <div class="product-subheader"><a href="/printing/sampletest/88">product two4</a></div>
    <div class="product-subheader"><a href="/printing/sampletest/99">In augue tellus2</a></div>
</div>

第二页

<script>
    $(document).ready(function(){
        alert($.cookie("product_name")); //cookie value is displayed


    $(".product-contents").hide();
        $("div.product-header").click(function(){
            $(this).toggleClass("active").next().slideToggle("fast");
        });

    });

如何使用cookie值从第一页打开所选元素打开或.show()第二页中的元素

1 个答案:

答案 0 :(得分:0)

尝试:

$("#product-list div").get( $.cookie("product_name") ).show();

虽然我认为您可能会在{id}设置和获取Cookie的位置$("#product-list > div")