JavaScript mootools addEvent

 ̄綄美尐妖づ 提交于 2019-12-23 18:18:08

问题


I am trying to find what's wrong with my code. Mootools core file is attached to HTML head and works well.

If I add the code:

$('myElement').addEvent('click', function(){
    alert('clicked!');
});

right below the <div id="myElement">Click me</div> then it works.

But if I add it to the separate javascript.js file then it does not work

HTML is this:

<!DOCTYPE html>
<html>  
<head>   
<script type="text/javascript" src="mootools-core-1.4.5-full-nocompat.js"></script>
<script type="text/javascript" src="javascript.js"></script>  
</head>
<body>  
<div id="myElement">Click me.</div>
</body>  
</html>

And javascript.js is this:

$('myElement').addEvent('click', function(){
    alert('clicked!');
});

All files (html, mootools-core and javascript.js are in the same directory). Any ideas?


回答1:


You have to wrap the expression in a domready event:

window.addEvent('domready', function() {
    $('myElement').addEvent('click', function() {
        alert('clicked!');
    });
});


来源:https://stackoverflow.com/questions/11629131/javascript-mootools-addevent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!