.show()无法正常工作

时间:2014-06-27 21:59:49

标签: javascript jquery html

我正在创建我正在创建的应用程序的问题。取决于我的变量是否为null应该显示特定的屏幕。一旦该变量附加了一个值,我的应用程序就会显示另一个div。但是,我的代码无效。看看下面的Javascript / Jquery:

(注意修改代码以显示null不是问题)

access_token = 3; 

   if (access_token == 3){
        $('.dashboardProfile').hide();
        $('.dashboardInfo').hide();
        $('.dashboardConnect').show();

    $('.connect').click(function () {
        var url = "https://foursquare.com/oauth2/access_token";
        url += "?client_id=" + foursquareApi.clientId;
        url += "&response_type=token";
        url += "&redirect_uri=" + foursquareApi.redirectUrl;

        window.location = url;
    });
}
else {
    $('.dashboardProfile').show();
    $('.dashboardInfo').show();
    $('.dashboardConnect').hide();
}

这是有问题的HTML:

<div class="dashboard-container">
    <div class="dashboardConnect">
        <div class="connect">
            <p>Sign In</p>
        </div>
    </div>
    <div class="dashboardProfile">
        <p>User Information and Picture</p>
    </div>
    <div class="dashboardInfo">
        <p>Check Ins</p>
        <div class="indicator checkin">#</div>
        <p>Countries</p>
        <div class="indicator country">#</div>
        <p>Cities</p>
        <div class="indicator city">#</div>
    </div>
</div>
</div>
<!--container-fluid-->

这是HTML的主管:

<head>
    <title>FourSquare Dashboard</title>
    <link rel="stylesheet" href="main.css">
    <script src="http://code.jquery.com/jquery-latest.min.js"></script>
    <script src="FourSquareJax.js"></script>
</head>

2 个答案:

答案 0 :(得分:0)

轻松修复。你做了一个小错字。将$('.dashboardConnect').hide();放在else循环中:

var access_token = 3;
if (access_token == 3) {       
    $('.dashboardProfile').hide();   
    $('.dashboardInfo').hide(); 
    $('.dashboardConnect').show();   
    $('.connect').click(function() {   
        var url = "https://foursquare.com/oauth2/access_token";
        url += "?client_id="+foursquareApi.clientId;
        url += "&response_type=token";
        url += "&redirect_uri="+foursquareApi.redirectUrl;
        window.location = url;
    });
}
else {
    $('.dashboardProfile').show();   
    $('.dashboardInfo').show();
    $('.dashboardConnect').hide();
}

答案 1 :(得分:0)

尝试将条件更改为:

if (!access_token)

如果access_token为空,未定义,空字符串或0,这将导致输入条件块。