2014-05-06 85 views
0

我使用jquery-1.11.1.min.js和我有此脚本:jQuery的饼干 - IF语句

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

我需要创建一个会记住你的页面加载选择(SHOWHIDE文本div class="text")的cookie。

我想我已经在function checkCookie()的错误,因为它忽略了饼干和div class="text"总是SHOW

你能帮帮我吗?

谢谢!

回答

0

我下面的评论你的代码:

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中始终使用===平等运营商和!==不平等操作。

+0

非常感谢! – Dvdrw