JavaScript Mootools addEvent [英] JavaScript mootools addEvent

查看:44
本文介绍了JavaScript Mootools addEvent的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试查找我的代码出了什么问题. Mootools核心文件已附加到HTML头,并且效果很好.

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

如果我添加代码:

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

<div id="myElement">Click me</div>的正下方,即可正常工作.

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

但是,如果我将其添加到单独的javascript.js文件中,则它将无法正常工作

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

HTML是这样的:

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

而javascript.js是这样的:

And javascript.js is this:

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

所有文件(html,mootools-core和javascript.js位于同一目录中). 有什么想法吗?

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

推荐答案

您必须将表达式包装在 domready中事件:

You have to wrap the expression in a domready event:

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

这篇关于JavaScript Mootools addEvent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆