JavaScript和OnClick事件,使类隐藏

JavaScript and OnClick Events, making classes hidden

本文关键字:隐藏 事件 OnClick JavaScript      更新时间:2023-09-26

我正试图使一个"计算器"作为实践,因为我正在学习Javascript。基本上,用户设置一个密码。设置密码后,用户需要再次输入密码。如果密码匹配,那么将出现一个计算器,密码框将消失。代码工作直到重新输入密码,我无法通过这部分,我被难住了!任何建议都将不胜感激。

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <script type="text/javascript">
            var pwd = prompt("Please enter your FAB password here", "");
            function DisplayCalc(pwd2) {
                var NewPwd = document.getElementById(pwd2);
                if (Newpwd == pwd) {
                    document.getElementsByClassName(NotVisibleClass).style.visibility = 'visible';
                    document.getElementsByClassName(VisibleClass).style.visibility = 'hidden';
                }
                else {
                    alert("Wrong Password Twinkle Toes!!");
                }
            }

        </script>
        <style>
            /* Need to style title to have a different color for every letter*/
            .Red{
                color: #f00;
            }
            .Orange{
                color: orange;
            }
            .Yellow{
                color: yellow;
            }
            .Green{
                color: green;
            }
            .Blue{
                color: blue;
            }
            .Purple{
                color: purple;
            }
            /*Need to have the background change colors*/
            .ResDivBy7{
                background-color: red;
            }
            .ResDivBy6{
                background-color: orange;
            }
            .ResDivBy5{
                background-color: yellow;
            }
            .ResDivBy4{
                background-color: green;
            }
            .ResDivBy3{
                background-color: blue;
            }
            .ResDivBy2{
                background-color: purple;
            }
            /*Need to hide elements */
            .NotVisibleClass{
                visibility: hidden;
            }
            /*Need to show elements*/
            .VisibleClass{
                visibility: visible;
            }
        </style>
        <title>Secret Rainbow Calculator!!!</title>
    </head>
    <body>
        <div class="header">
            <h1>
                <span class="Red">S</span><span class="Orange">E</span><span class="Yellow">C</span><span class="Green">R</span><span class="Blue">E</span><span class="Purple">T</span>
                <span class="Red">R</span><span class="Orange">A</span><span class="Yellow">I</span><span class="Green">N</span><span class="Blue">B</span><span class="Purple">O</span><span class="Red">W</span>
                <span class="Orange">C</span><span class="Yellow">A</span><span class="Green">L</span><span class="Blue">C</span><span class="Purple">U</span><span class="Red">L</span><span class="Orange">A</span><span class="Yellow">T</span><span class="Green">O</span><span class="Blue">R</span>
            </h1>
        </div>
        <div class="VisibleClass">
            <form action="">
                <fieldset>
                    <label>Please Enter Your FABULOUS Password!</label>
                    <input type="password"
                           id="pwd2" />
                    <button type="button" onClick= DisplayCalc(pwd2)>
                            Click Me When Done!</button>
                </fieldset>
            </form>
        </div>
        <div class="NotVisibleClass">
            <form action="">
                <fieldset>
                    <input type="text" id="calcScreen" readonly="readonly"/>
                    <br>
                    <button type="button"> 1 </button>
                    <button type="button"> 2 </button>
                    <button type="button"> 3 </button>
                    <button type="button"> 4 </button>
                    <br>
                    <button type="button"> 5 </button>
                    <button type="button"> 6 </button>
                    <button type="button"> 7 </button>
                    <button type="button"> 8 </button>
                    <br>
                    <button type="button"> Enter </button>
                    <button type="button"> 0 </button>
                    <button type="button"> 9 </button>
                    <button type="button"> Clear </button>
                    <br>
                    <button type="button"> + </button>
                    <button type="button"> - </button>
                    <button type="button"> * </button>
                    <button type="button"> / </button>    
                </fieldset>
            </form>
        </div>
    </body>
</html>

我的控制台说'Newpwd'在比较时没有定义(Newpwd == pwd)。看看你的源代码,你需要大写'Newpwd '中的'P'

<html lang="en">
    <head>
        <meta charset="utf-8" />
        <script type="text/javascript">
            var pwd = prompt("Please enter your FAB password here", "");
            function DisplayCalc(pwd2) {
                var NewPwd = document.getElementById(pwd2).value;
                if (NewPwd == pwd) {
                    document.getElementsByClassName('NotVisibleClass')[0].style.visibility  = 'visible';
                    document.getElementsByClassName('VisibleClass')[0].style.visibility = 'hidden';
                }
                else {
                    alert("Wrong Password Twinkle Toes!!");
                }
            }

        </script>
        <style>
            /* Need to style title to have a different color for every letter*/
            .Red{
                color: #f00;
            }
            .Orange{
                color: orange;
            }
            .Yellow{
                color: yellow;
            }
            .Green{
                color: green;
            }
            .Blue{
                color: blue;
            }
            .Purple{
                color: purple;
            }
            /*Need to have the background change colors*/
            .ResDivBy7{
                background-color: red;
            }
            .ResDivBy6{
                background-color: orange;
            }
            .ResDivBy5{
                background-color: yellow;
            }
            .ResDivBy4{
                background-color: green;
            }
            .ResDivBy3{
                background-color: blue;
            }
            .ResDivBy2{
                background-color: purple;
            }
            /*Need to hide elements */
            .NotVisibleClass{
                visibility: hidden;
            }
            /*Need to show elements*/
            .VisibleClass{
                visibility: visible;
            }
        </style>
        <title>Secret Rainbow Calculator!!!</title>
    </head>
    <body>
        <div class="header">
            <h1>
                <span class="Red">S</span><span class="Orange">E</span><span class="Yellow">C</span><span class="Green">R</span><span class="Blue">E</span><span class="Purple">T</span>
                <span class="Red">R</span><span class="Orange">A</span><span class="Yellow">I</span><span class="Green">N</span><span class="Blue">B</span><span class="Purple">O</span><span class="Red">W</span>
                <span class="Orange">C</span><span class="Yellow">A</span><span class="Green">L</span><span class="Blue">C</span><span class="Purple">U</span><span class="Red">L</span><span class="Orange">A</span><span class="Yellow">T</span><span class="Green">O</span><span class="Blue">R</span>
            </h1>
        </div>
        <div class="VisibleClass">
            <form action="">
                <fieldset>
                    <label>Please Enter Your FABULOUS Password!</label>
                    <input type="password"
                           id="pwd2" />
                    <button type="button" onClick="DisplayCalc('pwd2')">
                            Click Me When Done!</button>
                </fieldset>
            </form>
        </div>
        <div class="NotVisibleClass">
            <form action="">
                <fieldset>
                    <input type="text" id="calcScreen" readonly="readonly"/>
                    <br>
                    <button type="button"> 1 </button>
                    <button type="button"> 2 </button>
                    <button type="button"> 3 </button>
                    <button type="button"> 4 </button>
                    <br>
                    <button type="button"> 5 </button>
                    <button type="button"> 6 </button>
                    <button type="button"> 7 </button>
                    <button type="button"> 8 </button>
                    <br>
                    <button type="button"> Enter </button>
                    <button type="button"> 0 </button>
                    <button type="button"> 9 </button>
                    <button type="button"> Clear </button>
                    <br>
                    <button type="button"> + </button>
                    <button type="button"> - </button>
                    <button type="button"> * </button>
                    <button type="button"> / </button>    
                </fieldset>
            </form>
        </div>
    </body>
</html>

在以下部分:

 if (Newpwd == pwd) {
                    document.getElementsByClassName(NotVisibleClass).style.visibility = 'visible';
                    document.getElementsByClassName(VisibleClass).style.visibility = 'hidden';
                }

change 'Newpwd == pwd' to 'NewPwd.value == pwd'

你的代码仍然需要一些css相关的修改。

我建议你开始使用一些开发工具(如Chrome浏览器的开发工具)。 只需在所需页面上按F12,在Google Chrome中,打开开发人员工具页面,并探索您的代码。

正如Justin H所提到的,在if语句中使用的变量'NewPwd'中的' p '有一个小写的p . JavaScript是一种大小写敏感的语言,因此Newpwd是未定义的。把if语句中的P大写,你就可以重新开始运行了。此外,您只是通过ID获取文档元素,这不会返回它的值。它将返回一个对象。因此,您还需要修改变量NewPwd的定义。

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <script type="text/javascript">
            var pwd = prompt("Please enter your FAB password here", "");
            function DisplayCalc(pwd2) {
             //Variable NewPwd with capital 'P'
                var NewPwd = document.getElementById(pwd2).value;
              //Use of Variable NewPwd used without capitalization of 'P'
                if (Newpwd == pwd) { //Newpwd == pwd returns undefined
                    document.getElementsByClassName(NotVisibleClass).style.visibility = 'visible';
                    document.getElementsByClassName(VisibleClass).style.visibility = 'hidden';
                }
                else {
                    alert("Wrong Password Twinkle Toes!!");
                }
            }

        </script>
        <style>
            /* Need to style title to have a different color for every letter*/
            .Red{
                color: #f00;
            }
            .Orange{
                color: orange;
            }
            .Yellow{
                color: yellow;
            }
            .Green{
                color: green;
            }
            .Blue{
                color: blue;
            }
            .Purple{
                color: purple;
            }
            /*Need to have the background change colors*/
            .ResDivBy7{
                background-color: red;
            }
            .ResDivBy6{
                background-color: orange;
            }
            .ResDivBy5{
                background-color: yellow;
            }
            .ResDivBy4{
                background-color: green;
            }
            .ResDivBy3{
                background-color: blue;
            }
            .ResDivBy2{
                background-color: purple;
            }
            /*Need to hide elements */
            .NotVisibleClass{
                visibility: hidden;
            }
            /*Need to show elements*/
            .VisibleClass{
                visibility: visible;
            }
        </style>
        <title>Secret Rainbow Calculator!!!</title>
    </head>
    <body>
        <div class="header">
            <h1>
                <span class="Red">S</span><span class="Orange">E</span><span class="Yellow">C</span><span class="Green">R</span><span class="Blue">E</span><span class="Purple">T</span>
                <span class="Red">R</span><span class="Orange">A</span><span class="Yellow">I</span><span class="Green">N</span><span class="Blue">B</span><span class="Purple">O</span><span class="Red">W</span>
                <span class="Orange">C</span><span class="Yellow">A</span><span class="Green">L</span><span class="Blue">C</span><span class="Purple">U</span><span class="Red">L</span><span class="Orange">A</span><span class="Yellow">T</span><span class="Green">O</span><span class="Blue">R</span>
            </h1>
        </div>
        <div class="VisibleClass">
            <form action="">
                <fieldset>
                    <label>Please Enter Your FABULOUS Password!</label>
                    <input type="password"
                           id="pwd2" />
                    <button type="button" onClick= DisplayCalc(pwd2)>
                            Click Me When Done!</button>
                </fieldset>
            </form>
        </div>
        <div class="NotVisibleClass">
            <form action="">
                <fieldset>
                    <input type="text" id="calcScreen" readonly="readonly"/>
                    <br>
                    <button type="button"> 1 </button>
                    <button type="button"> 2 </button>
                    <button type="button"> 3 </button>
                    <button type="button"> 4 </button>
                    <br>
                    <button type="button"> 5 </button>
                    <button type="button"> 6 </button>
                    <button type="button"> 7 </button>
                    <button type="button"> 8 </button>
                    <br>
                    <button type="button"> Enter </button>
                    <button type="button"> 0 </button>
                    <button type="button"> 9 </button>
                    <button type="button"> Clear </button>
                    <br>
                    <button type="button"> + </button>
                    <button type="button"> - </button>
                    <button type="button"> * </button>
                    <button type="button"> / </button>    
                </fieldset>
            </form>
        </div>
    </body>
</html>