未定义添加事件

时间:2014-07-08 06:57:24

标签: javascript

http://code.tutsplus.com/tutorials/the-basics-of-object-oriented-javascript--net-7670

上面的教程告诉我们:

$(document).ready(function(){
    //all our code that runs after the page is ready goes here
});

 addEvent(window, 'load', function(){
        //all our code that runs after the page is ready goes here
    });

是一样的。所以我试图像这样使用addEvent:

<html>
<head>

<script type="text/javascript">

addEvent(window, 'load', function(){
    alert("test");
});
</script>

</head>
<body>

<form id="ourForm">
    <label>First Name</label><input type="text" /><br />
    <label>Last Name</label><input type="text" /><br />
    <label>Email</label><input type="text" /><br />
    <input type="submit" value="submit" />
</form>

</body>

</html> 

但我收到错误: Uncaught ReferenceError: addEvent is not defined

请帮忙。我是javascript的新手

1 个答案:

答案 0 :(得分:0)

教程包含addEvent功能。请检查。

function addEvent(to, type, fn){
    if(document.addEventListener){
        to.addEventListener(type, fn, false);
    } else if(document.attachEvent){
        to.attachEvent('on'+type, fn);
    } else {
        to['on'+type] = fn;
    }  
};

您可以使用Jquery事件代替此方法。

http://api.jquery.com/category/events/