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

问题描述

我有一个奇怪的错误,我不明白.我只是将代码从jQuery移到Mootools,因为它比jQuery的混乱情况干净得多.

I've strange error i don't understand. I'm just moving my code from jQuery over to Mootools because it's much cleaner than the jQuery mess.

现在,当我使用

$$('div.canvas')

在mootools中,我得到了正确的元素.

in mootools i get the correct element.

当我尝试

$(document).getElement('div.canvas')

它告诉我 $ 没有定义.如何定义 $$ 和所有辅助功能(如 $ lambda 等),而不是 $ ?

it tells me that $ is not defined. How can $$ and all helper functions like $lambda etc. be defined but not $?

那里的内容从 1.1更改为1.2 ,并且文档尚未更新吗?

Has something changed there from 1.1 to 1.2 and the docs are not updated yet?

推荐答案

定义$时,mootools 1.2.3+不会接管它,而是恢复为使用document.id.在该版本之前,这种情况以前从未发生过,因此在很大程度上取决于您所引用的版本.但自1.11以来肯定已更改,并且已记录在案,请在此处阅读公告 http://mootools.net/blog/2009/06/22/the-dollar-safe-mode/

as someone pointed out, when $ is defined, mootools 1.2.3+ will not take it over, it will revert to using document.id instead. this did not used to happen before that release so it largely depends on the version you are referencing. but it's certainly changed since 1.11 and it IS documented, read the announcement here http://mootools.net/blog/2009/06/22/the-dollar-safe-mode/

对于您的应用程序设计,这意味着,如果您的结构是...

to your application design this means that, if your structure is...

加载jquery(无需冲突,没有关系)
加载mootools

load jquery (no need for noconflict, does not matter)
load mootools

...它可以按如下方式工作:

... it can work as follows:

$("#foo"); // jquery
document.id("foo"); // mootools

// or create a temporary scope for mootools things
(function($) {
    $("foo"); // mootools
})(document.id);

mootools开发中的最佳/最新实践要求发布插件和代码以引用document.id或位于此类闭包内以确保兼容性.这实际上是可以的,就像在jquery中不同,$在jQuery单例中使用$作为别名,在mootools中$只是一个选择器,因此它的使用范围要小得多.因此,键入document.id("selector")不会带来太多麻烦.

best / recent practices in mootools development require plugins and code released to reference document.id or be within such a closure to ensure compatibility. this is actually ok as unlike in jquery where $ aliases the jQuery singleton, in mootools $ is just a selector so its use will be far less spread. Hence typing document.id("selector") is not going to be that much of a drag.

这篇关于mootools:$未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-13 02:20