发出确认窗口

时间:2017-07-15 19:16:32

标签: javascript html

在HW项目上工作,我已经一步一步地按照它来尝试让这个确认窗口弹出但无法弄明白。我没有收到任何控制台错误,因此我认为我的代码只是在某个地方被破坏了。



<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="styles.css" />
    <script src="modernizr.custom.05819.js"></script>
</head>

<body>
    <article>
        <h2>Change of address form</h2>
        <form>
            <fieldset id="contactinfo">
                <label for="addrinput">
            Street Address
          </label>
                <input type="text" id="addrinput" name="Address" />
                <label for="cityinput">
            City
          </label>
                <input type="text" id="cityinput" name="City" />
                <label for="stateinput">
            State/Province
          </label>
                <input type="text" id="stateinput" name="State" />
                <label for="zipinput">
            Zip/Postal Code
          </label>
                <input type="number" id="zipinput" name="Zip" />
            </fieldset>
            <fieldset id="submitsection">
                <input type="button" id="submit" value="Submit" />
            </fieldset>
        </form>

    </article>
    <script>
        function processInput() {
            var propertyWidth = 300;
            var propertyHeight = 100;
            var winLeft = ((screen.width - propertyWidth) / 2);
            var winTop = ((screen.height - propertyHeight) / 2);
            var winOptions = "width=300,height=100";
            winOptions += ",left=" + winLeft;
            winOptions += ",top=" + winTop;
            window.open("confirm.htm", "confirm", winOptions);
        }

        function createEventListener() {
            var submitButton = document.getElementById("submit");
            if (submitButton.addEventListener) {
                submitButton.addEventListener("click", processInput,
                    false);
            } else if (submitButton.attachEvent) {
                submitButton.attachEvent("onclick", processInput);
            }
        }

        if (window.addEventListner) {
            window.addEventListener("load", createEventListener, false);
        } else if (window.attachEvent) {
            window.attachEvent("onload", createEventListener);
        }
    </script>
</body>

</html>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

将此代码移至createEventListener之外:

if (window.addEventListner) {
    window.addEventListener("load", createEventListener, false);
} else if (window.attachEvent) {
    window.attachEvent("onload", createEventListener);
}