Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jquerycookies-IF语句_Javascript_Jquery_If Statement_Cookies - Fatal编程技术网

Javascript jquerycookies-IF语句

Javascript jquerycookies-IF语句,javascript,jquery,if-statement,cookies,Javascript,Jquery,If Statement,Cookies,我使用的是jquery-1.11.1.min.js,我有以下脚本: <script type="application/javascript" src="jquery-1.11.1.min.js"></script> <script> function setCookie(cname,cvalue,exdays) { var d = new Date(); d.setTime(d.getTime()+(exdays*24*60*60*1000)); var

我使用的是
jquery-1.11.1.min.js
,我有以下脚本:

<script type="application/javascript" src="jquery-1.11.1.min.js"></script> 
<script>

function setCookie(cname,cvalue,exdays)
{
var d = new Date();
d.setTime(d.getTime()+(exdays*24*60*60*1000));
var expires = "expires="+d.toGMTString();
document.cookie = cname+"="+cvalue+"; "+expires;
}

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

function checkCookie()
{
var x=getCookie("cookiename");
if (x!="")
  {

            if (x="no")
                    {
                        $('.text').hide('slow'); 
                        $('#less').attr('style','display: none;');
                        $('#more').attr('style','display: block;');
                    }

            if (x="yes")
                    {
                        $('.text').show('slow');
                        $('#less').attr('style','display: block;');
                        $('#more').attr('style','display: none;');
                    }

  }


else 
  {

    setCookie("cookiename","no",30);
    $('.text').hide('slow'); 
    $('#less').attr('style','display: none;');
    $('#more').attr('style','display: block;');
  }
}

</script>

lorem ipsum
<div class="text">
 SOME TEXT
</div>

  <div id="less">
 <a href="#" class="hidemore">Hide more</a>
 </div>
 <div id="more">
  <a href="#" class="showmore">Show more</a>
 </div>


  <script type="text/javascript">
  $(document).ready(function() {
    checkCookie();
    });

    $('.showmore').click(function() {
     $('.text').show('slow');
     $('#less').attr('style','display: block;');
     $('#more').attr('style','display: none;');
     setCookie("cookiename","yes",30);

    });

    $('.hidemore').click(function() {
      $('.text').hide('slow'); 
      $('#less').attr('style','display: none;');
     $('#more').attr('style','display: block;');
     setCookie("cookiename","no",30);

    });
  </script>

函数setCookie(cname、cvalue、exdays)
{
var d=新日期();
d、 设置时间(d.getTime()+(exdays*24*60*60*1000));
var expires=“expires=“+d.togmString();
document.cookie=cname+“=”+cvalue+”;“+expires;
}
函数getCookie(cname)
{
变量名称=cname+“=”;
var ca=document.cookie.split(“;”);

对于(var i=0;i我已在下面对您的代码进行了注释:

if (x!="") // if x is not ""  - let's assume it's true
{

  if (x="no") // here you assign x value "no" - so the if is similar to if("no")
  {
  // since x="no" happens to be "truthy" this block code will execute and will HIDE the div with class .text
    $('.text').hide('slow'); 
    $('#less').attr('style','display: none;');
    $('#more').attr('style','display: block;');
  }

  if (x="yes") //here you assign x value "yes" - again this is similar to writing if("yes")
  {
  // since x="yes" is again "truthy" this code block will execute also SHOWING the div with class .text
    $('.text').show('slow');
    $('#less').attr('style','display: block;');
    $('#more').attr('style','display: none;');
  }

}
因此,您应该切换到
x==“no”
x==“yes”
,或者在javascript中始终使用
==
表示相等运算符,使用
!=
表示不等运算符