原型和jQuery与$ .when.apply冲突 [英] Prototype and jQuery conflict with $.when.apply
问题描述
我已经尝试过多种使用代码的组合,以使Prototype和jQuery能够正常工作,但是还算不上成功.
I have tried multiple combination of usage of code to make the Prototype and jQuery to work, but no luck yet.
这是我目前所拥有的.
index.html:
index.html:
<script src="https://ajax.googleapis.com/ajax/libs/prototype/1.7.2.0/prototype.js"></script>
<script src="http://code.jquery.com/jquery.js"></script>
<script src="MAIN_JS_FILE.js"></script>`
main_js_file.js:
main_js_file.js:
jQuery(document).ready(function() {
var jsq = [];
jsq.push(AJAX_CALL_FOR_FETCHING_JS_FILE_1);
jsq.push(AJAX_CALL_FOR_FETCHING_JS_FILE_2);
jsq.push(AJAX_CALL_FOR_FETCHING_JS_FILE_3);
var deferredjs = jQuery.when.apply(jQuery, jsq);
deferredjs.done(function() {
//Various variable initialization
//Various function definition.
}
});
现在,当页面加载(页面正确加载)时,Chrome控制台显示错误消息:
Now when page is loaded (page loads properly), chrome console shows an error message:
未捕获的TypeError:未定义不是函数.
Uncaught TypeError: undefined is not a function.
当点击错误文件链接时,它指向prototype.js文件中的element.dispatchEvent(event);
,行号7066.
When clicked on the error file link, it points to element.dispatchEvent(event);
in the prototype.js file, line no 7066.
感谢您的帮助.
谢谢.
我将MAIN_JS_FILE.js
文件更改为仅使用jQuery
而不是$
因此,现在没有一个使用$
的javascript代码,并且仍然显示undefined
错误.
I have changed the MAIN_JS_FILE.js
file to use only jQuery
instead of $
So now there is not a single javascript code that uses $
and still the undefined
error is displayed.
如果我现在在.ready()
函数之前使用jQuery.noConflict();
,那么$.when.apply
代码甚至无法执行.
If I now use jQuery.noConflict();
before .ready()
function, then the $.when.apply
code does not even execute.
推荐答案
正如注释中提到的@steven iseki
,可以使用 jQuery.noConflict
.您可以在他们的网站上阅读以下内容:
As @steven iseki
mentioned in comment you, can use jQuery.noConflict
. You can read on their website that:
许多JavaScript库都使用$作为函数或变量名,就像jQuery一样.在jQuery的情况下,$只是jQuery的别名,因此无需使用$即可使用所有功能.
Many JavaScript libraries use $ as a function or variable name, just as jQuery does. In jQuery's case, $ is just an alias for jQuery, so all functionality is available without using $.
Prototype
确实也使用$
符号.
此外,请记住使用jQuery
而不是$
登录jQuery代码,例如:
Also, remember to use jQuery
instead of $
sign in your jQuery code, e.g.:
jQuery(selector).on('click', function(){...});
这篇关于原型和jQuery与$ .when.apply冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!