javascript - 确认窗口的问题

标签 javascript html

在一个硬件项目上工作,我一步一步地尝试让这个确认窗口弹出,但无法弄清楚。我没有收到任何控制台错误,所以我假设我的代码只是在某个地方损坏了。

<!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>

最佳答案

将此代码移到 createEventListener 之外:

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

关于javascript - 确认窗口的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45121904/

相关文章:

html - 当用户单击 “add to home screen” 时,我可以向我的 iPhone 目标 Web 应用程序添加其他内容吗?

javascript - 使用 z-index 剪切标签

html - IE中的提交按钮和显示 block

jQuery在点击时改变div的大小

html - Bootstrap 导航选项卡显示所有主体

html - 如何在 html 中划分 3 个相等的部分 li

javascript - 设置高度 div javascript

javascript - 调用函数 ogads load.php

javascript - getElementById/InnerHTML 函数不起作用

javascript - 在 knockout 应用程序中使用 setInterval 进行轮询实现?