2010-03-13 27 views
2

我有奇怪的錯誤,我不明白。我只是將我的代碼從jQuery移到Mootools,因爲它比jQuery混亂得多。

現在,當我使用

$$('div.canvas') 
在mootools的

我得到正確的元素。

當我嘗試

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

它告訴我,$沒有定義。 $$$ lambda等所有幫助功能如何定義但不是$

1.1到1.2有什麼變化嗎和文檔沒有更新?

+0

我正在使用jQuery與noConflict選項,而我重新實現了所有的代碼。將我之前的所有$ ref更改爲jQuery。 noConflict選項有問題嗎? –

+0

奇怪...我現在雙重檢查,如果從jQuery的$遺留下來,但沒有。我甚至在jQuery之後包含了mootools框架,這樣它就可以覆蓋$但不起作用。 雖然當我刪除所有的jQuery包括它的作品。 似乎沒有衝突()不遵守它的承諾? –

+0

更新了noConlflict()示例的答案 – Andy

回答

2

正如有人指出的那樣,當$被定義時,mootools 1.2.3+不會接管它,它會恢復使用document.id代替。這在發佈之前並沒有發生過,因此它在很大程度上取決於您所引用的版本。但它自1.11肯定改變,據記載,讀到這裏http://mootools.net/blog/2009/06/22/the-dollar-safe-mode/

公佈到您的應用程序設計,這意味着,如果你的結構是...

負荷的jQuery(無需noconflict,無所謂)
負載的mootools

...它可以工作方式如下:在mootools的發展

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

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

最佳/最近的做法,需要插件和代碼R選擇引用document.id或在這種封閉之內以確保兼容性。這實際上是可以的,因爲與在jquery中不同,jquery獨立於jQuery單例,在mootools中,$就是一個選擇器,所以它的使用將會少得多。因此,輸入document.id(「選擇器」)不會有太大的阻力。

+0

雖然它沒有解決我的問題,但我將獎勵發給你;),恭喜。最後,將所有代碼移動到moo的速度更快,並且現在忽略問題以解決衝突本身。 –

1

你在你的htmls中刪除了所有對jQuery的引用嗎?

+0

是的,我使用noConflict(見上面) –

2

如果您使用的是同一頁上兩個庫必須使用JQuery's noConflict() function

<script type="text/javascript" src="other_lib.js"></script> 
<script type="text/javascript" src="jquery.js"></script> 
<script type="text/javascript"> 
    $.noConflict(); 
    jQuery(document).ready(function($) { 
    // Code that uses jQuery's $ can follow here. 
    }); 
    // Code that uses other library's $ can follow here. 
</script> 

如果您仍然有問題,嘗試通過您包括JQuery的文件檢查,以確保任何插件/代碼使用jQuery('div.canvas')等代替$由於$已由noConflict()函數發佈,並且不會運行JQuery代碼。

+0

是的,我使用noConflict(見上面) –

1

如果MooTools已經存在,它將不會覆蓋$函數。在定義它之前它會檢查$的無效性。所以我懷疑美元仍然潛伏在某個地方。

if (window.$ == null) Window.implement({ 
    $: function(el, nc){ 
     return document.id(el, nc, this.document); 
    } 
}); 

包括jQuery和運行$.noConflict();後,但包括MooTools的面前,你可以登錄的$內容,看到什麼記錄?

include jquery 
$.noConflict(); 
console.log($); // should return undefined 
include mootools 
+0

當我這樣做: jQuery.noConflict(); console.log(「jQuery there?」); console.log($); console.log(jQuery);它說: 「jQuery在那裏?」 undefined 函數(a,b).... 因此,jQuery的定義和$不是 –

+0

是否有其他插件在頁面上或只是核心庫? – Anurag

+0

還有jquery和mootools,而我正在遷移到moo,以及用於使用google.load('maps','2')加載gmaps的google api加載器。 當然,我自己的framework.js文件,我三重檢查了剩餘$'s但沒有。 –