我的简单代码无法正常工作。它说那个人不是构造函数,即使我很确定也是如此。请帮我。由于某种原因,我只是无法理解。

<!DOCTYPE html>
    <html>
    <head>
        <title>...</title>
        <script type="text/javascript">
            var people = 0;
            function person(name, age, colour){
                this.name = name;
                this.age = age;
                this.colour = colour;
            }

            function triggerNew(){
                var s = document;
                s.getElementById("hiddenPers").style = "opacity: 100;"
            }

            function submitPerson(){
                var n = document.getElementById("name").value;
                var a = document.getElementById("age").value;
                var c = document.getElementById("colour").value;
                people++;
                var person = new person(n, a, c);
                alert(person1);
                document.getElementById("hiddenPers").style = "opacity: 0;"
            }

        </script>
        <style type="text/css">

        </style>
    </head>
    <body>
    <button id="newPerson" onclick="triggerNew()">New Person!</button>
    <div id="hiddenPers" style="opacity: 0;">
        <input type="text" id="name" placeholder="Name:"><br>
        <input type="text" id="age" placeholder="Age: "><br>
        <input type="text" id="colour" placeholder="Favourite Colour:">
        <button id="submitPerson" onclick="submitPerson()">Submit Person! `</button>`
    </div>
    </body>
    </html>

最佳答案

更改此行

var person = new person(n, a, c);



var person1 = new person(n, a, c);


您的密码


          var people = 0;
            function person(name, age, colour){
                this.name = name;
                this.age = age;
                this.colour = colour;
            }

            function triggerNew(){
                var s = document;
                s.getElementById("hiddenPers").style = "opacity: 100;"
            }

            function submitPerson(){
                var n = document.getElementById("name").value;
                var a = document.getElementById("age").value;
                var c = document.getElementById("colour").value;
                people++;
                var person1 = new person(n, a, c);
                alert(person1.name);
                document.getElementById("hiddenPers").style = "opacity: 0;"
            }

<!DOCTYPE html>
    <html>
    <head>
        <title>...</title>
        <script type="text/javascript">


        </script>
        <style type="text/css">

        </style>
    </head>
    <body>
    <button id="newPerson" onclick="triggerNew()">New Person!</button>
    <div id="hiddenPers" style="opacity: 0;">
        <input type="text" id="name" placeholder="Name:"><br>
        <input type="text" id="age" placeholder="Age: "><br>
        <input type="text" id="colour" placeholder="Favourite Colour:">
        <button id="submitPerson" onclick="submitPerson()">Submit Person! `</button>`
    </div>
    </body>
    </html>

关于javascript - “人不是构造函数”,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44219590/

10-10 04:55