隐藏HTML直到if语句

时间:2013-04-10 11:41:16

标签: javascript

我最近一直试图在我的网站上设置一个简单的帐户系统。如果您有帐户,则会显示游戏,如果您没有,则该页面会显示You need an account!

在if语句之前,我似乎无法弄清楚如何隐藏HTML嵌入代码。

到目前为止,这是我的代码:

<html>
    <body>
        <div id="Game">
            <center>
                <object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="700" height="600" align="middle" id="kingdom-rush-1-082">
                    <param name="movie" value="http://m.toogame.com/k/swfs/kingdom-rush-1-082_80r.swf">
                    <param name="quality" value="high">
                    <param name="AllowScriptAccess" value="always">
                    <!--[if !IE]>-->
                        <object type="application/x-shockwave-flash" data="http://m.toogame.com/k/swfs/kingdom-rush-1-082_80r.swf" width="700" height="600">
                            <param name="movie" value="http://m.toogame.com/k/swfs/kingdom-rush-1-082_80r.swf">
                            <param name="quality" value="high">
                            <param name="AllowScriptAccess" value="always">
                        <!--<![endif]-->
                        <!--[if !IE]>-->
                        </object>
                    <!--<![endif]-->
                </object>
        </div>
        <script type="text/javascript">
            var pass = window.prompt("Type Your Password")
             if ((pass == 'bacon'))
                 document.getElementById('Game').style.display = "none"
            else 
                 document.write("You need an account for this!")
        </script>
    </body>
</html>

4 个答案:

答案 0 :(得分:1)

document.getElementById('Game').style.display = "block"

任何拥有浏览器,密码都很好的人都可以查看密码。

diplsay:block表示可见 display:none它不可见。

答案 1 :(得分:1)

首先使用样式

隐藏html元素
<div id="Game" style="display: none">

然后在脚本中

var pass= window.prompt("Type Your Password")
if ( (pass=='bacon') )
    document.getElementById('Game').style.display = "block"
else
    document.write("You need an account for this!")

答案 2 :(得分:0)

对nimchimpsky / arun p johny的解决方案(center元素正确终止)的小调整。如果您选择接受我的答案,您应该接受其中一位指定贡献者的答案。

<html>
    <body>
        <div id="Game" style="display:none;">
            <center>
                <object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="700" height="600" align="middle" id="kingdom-rush-1-082">
                    <param name="movie" value="http://m.toogame.com/k/swfs/kingdom-rush-1-082_80r.swf">
                    <param name="quality" value="high">
                    <param name="AllowScriptAccess" value="always">
                    <!--[if !IE]>-->
                        <object type="application/x-shockwave-flash" data="http://m.toogame.com/k/swfs/kingdom-rush-1-082_80r.swf" width="700" height="600">
                            <param name="movie" value="http://m.toogame.com/k/swfs/kingdom-rush-1-082_80r.swf">
                            <param name="quality" value="high">
                            <param name="AllowScriptAccess" value="always">
                        <!--<![endif]-->
                        <!--[if !IE]>-->
                        </object>
                    <!--<![endif]-->
                </object>
            </center>
        </div>
        <script type="text/javascript">
            var pass = window.prompt("Type Your Password")
             if ((pass == 'bacon'))
                 document.getElementById('Game').style.display = "block"
            else 
                 document.write("You need an account for this!")
        </script>
    </body>
</html>

答案 3 :(得分:0)

看过您的网站后,很明显您计划使用多个密码。然后,我建议使用数组来存储密码,然后检查输入是否包含在数组中。

<强> Working Demo

HTML:

<div id="result">Waiting for password input...</div>
<div id="hiddenContent" style="display: none">This content is normally hidden unless a user has entered the correct password. Using Javascript is not a very good way to secure content though, as it can easily be bypassed.</div> 

使用Javascript:

var passwords = new Array();
passwords[0] = "pass1";
passwords[1] = "pass2";
passwords[2] = "pass3";
// This is your array of passwords. You can add as many as you want!

var passwordInput=prompt("Please enter your password...");
// This will ask for the user to enter their password

if (inArray(passwordInput, passwords)===true) {
    // This uses the inArray function to check if the users input is found within the array.
    document.getElementById("result").innerHTML="password found";
    document.getElementById('hiddenContent').style.display = "block"
    // If the password is found then it sets content of the <div> and makes it possible to view the hidden content.
}
else {
    document.getElementById("result").innerHTML="password not found";
    // If the password is not found then it sets the content of the <div>
}

// This function checks if a variable is found within an array
function inArray(variable, array) {
    var length = array.length;
    for(var i = 0; i < length; i++) {
        if(array[i] == variable) return true;
    }
    return false;
}