1

スライドを使用して、ボタンを使用して非表示/表示としてボックスを切り替えるために、このコードを記述しました。

jQuery("#button").click(function () {
    jQuery('#box').slideToggle('fast');
});

ボックスが非表示か表示かを記憶するために、これにCookieを実装したいと思います。誰かがこれで私を導くことができますか?

4

2 に答える 2

3

利用可能なjQueryCookieプラグインがあります。これにより、Cookieの読み取りと書き込みがはるかに簡単で便利になります。次に例を示します。

// if the slider is visible, set the cookie slider value to true
$.cookie('slider', 'visible', { expires: 7, path: '/' });

値を読み取るには、例として以下を使用します。

var sliderVisible = $.cookie('slider');

詳細については、jQueryCookiesプラグインサイトを参照してください。

于 2012-05-26T08:36:48.653 に答える
1

私はちょうどそれを動かしました。それぞれの状態(つまり、閉じている状態と開いている状態)で異なるボタンが表示されるように、2つのボタンを作成しました。

jQuery('#button_open').hide(); //initially we keep the open button hidden

//このコードは、閉じるボタンをクリックしたときに何が起こるかを定義します

jQuery('#button_close').click(function () {
      jQuery(this).hide(); //this hides the close button as the box is now closed
      jQuery('#box').slideUp('fast'); //hides the box
      jQuery('#button_open').show(); //shows the open button
      jQuery.cookie("openclose","closed", {expires: 365}); // sets cookie
      return false;
    });

//このコードは、開くボタンをクリックしたときに何が起こるかを定義します

jQuery("#button_open").click(function () {
      jQuery(this).hide(); //hides the open button as the box is now open
      jQuery('#box').slideDown('fast'); //shows the box
      jQuery('#button_close').show(); //shows the close button
      jQuery.cookie("openclose","open", {expires: 365}); //sets cookie
      return false;
    });

//そして魔法の部分が入ります。このコードは「openclose」という名前のCookieの値が「closed」であるかどうかをチェックします。はいの場合、閉じるボタンとボックスを非表示にし、開くボタンを表示します。

    if(jQuery.cookie("openclose") == "closed") {
        jQuery("#button_close").hide();
        jQuery("#button_open").show();
        jQuery('#box').hide();
    };
于 2012-05-27T06:51:20.747 に答える