0

I am following this link to learn about basic javascript but for some strange reason this little piece of code does not seem to be playing nice with me. Any suggestions on why I am not getting the alerts would be appreciated

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="content-type" content="text/html; charset=utf-8" />
        <title>Creeper</title>
        <script type="text/javascript">
            addEvent(window, "load", sayHi);
            function sayHi(){
                alert("jekyl");
            }
        </script>
    </head>
    <body>
        <a href="#" title="click me", id="my-link">Click Me!</a>
    </body>
</html>
4

2 回答 2

3

addEvent 不是在window. 您正在关注的文章引用了它,但假设您首先加载了一个实现,而您在发布的代码中没有这样做。您可以尝试 Dean Edwards 实施(上面评论中的链接)或尝试以下操作:

   <script type="text/javascript">
        window.onload = function() {
            alert("jekyl");
        }

    </script>
于 2013-06-14T01:37:54.993 回答
0

您也可以使用事件监听器:

if (window.addEventListener){
    window.addEventListener('load', sayHi, false); // All browsers except IE
} else if (window.attachEvent && !window.opera){
    window.attachEvent('onload', sayHi); // IE
}

function sayHi(){
    alert("jekyl");
}
于 2013-06-14T09:46:03.630 回答