隐藏/显示永久的块(Hide/show a block permanently)

2019-10-31 13:05发布

我使用的slideToggle打开和关闭格,与使用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文心不是改变,从显示隐藏,反之亦然。我必须失去了一些东西很简单..

开溜也意味着要问,我怎么阿塔赫一个cookie事件

Answer 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>


Answer 2:

住在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>


文章来源: Hide/show a block permanently
标签: jquery toggle