永久隐藏/显示一个块

时间:2011-12-11 06:23:14

标签: jquery toggle

我使用slidetoggle打开和关闭div,使用jquery缓动。

<script>
 function toggleText() {
 $('#divToToggle').slideToggle("slow");
 if ($('#divToToggle').css('display') == 'none') {
      $('#aTag').html('Show');
 }
 else {
      $('#aTag').html('Hide');
 }
 }
</script>
<a id="aTag" href="javascript:toggleText();">
  Hide
</a>

它控制的div是<div id="divToToggle"></div>

问题我是onclick文本没有变化,从显示到隐藏,反之亦然......我必须错过一些非常简单的东西..

Bugger也想问一下,我如何获得活动的cookie

2 个答案:

答案 0 :(得分:1)

直播JSFiffle http://jsfiddle.net/T7vFf/

<script>
function toggleText() {
    $('#divToToggle').slideToggle("300", function() {
        if ($(this).is(":visible")) {
            $('#aTag').html('Hide');
        } else {
            $('#aTag').html('Show');
        }
    });
}
</script>

<a id="aTag" href="javascript:toggleText();">
  Hide
</a>
<div id="divToToggle">Div To Toggle</div>

答案 1 :(得分:1)

试试这个。这将有效

<script>
     function toggleText() {
          $('#divToToggle').slideToggle("slow", function(){
             if ($('#divToToggle').css('display') == 'none') {
                 $('#aTag').html('Show');
             }
             else 
             {
                 $('#aTag').html('Hide');
             }    
       });    
     }
</script>
<a id="aTag" href="javascript:toggleText();">
      Hide
</a>

编辑: 这是一个关于如何设置和删除cookie的简单函数 - src -

  function setCookie(name,value,days) {
        if (days) {
            var date = new Date();
            date.setTime(date.getTime()+(days*24*60*60*1000));
            var expires = "; expires="+date.toGMTString();
        }
        else var expires = "";
        document.cookie = name+"="+value+expires+"; path=/";
    }

    function getCookie(name) {
        var nameEQ = name + "=";
        var ca = document.cookie.split(';');
        for(var i=0;i < ca.length;i++) {
            var c = ca[i];
            while (c.charAt(0)==' ') c = c.substring(1,c.length);
            if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
        }
        return null;
    }

    function deleteCookie(name) {
        setCookie(name,"",-1);
    }

   function toggleText() {
              $('#divToToggle').slideToggle("slow", function(){
                 if ($('#divToToggle').css('display') == 'none') {
                     $('#aTag').html('Show');
                      deleteCookie('hiddendiv') 

                 }
                 else 
                 {
                     $('#aTag').html('Hide');
                     setCookie('hiddendiv',1,MENTOINYOURTIMEHERE)  
                 }    
           });    
         }

  $('aTag').click('toggleText');
  if(getCookie('hiddendiv'))
  {
    $('#divToToggle').hide();
  }

<a id="aTag" href="javascript:toggleText();">
      Hide
</a>
<div id ="divToToggle"></div>