为什么我需要两次加载 jquery [英] Why do I need to load jquery twice
问题描述
在我的 html 页面中,我需要将 jquery 放入两次.如果我将其中任何人从我的页面中取出,则该页面不会被呈现.请注意,包含的jquery"都指向相同的位置.
In my html page, I need to put jquery twice. If I take anyone of them out my page , then the page does not get rendered. Kindly note that both the included "jquery" are pointing to same location.
我正在使用backbone.js.我添加了 require.js 以在页面底部包含 jquery.另外,我需要在页面的开头添加 jquery.一旦我添加了这两个,我的整个页面就会正确呈现.如果我把它们中的任何一个拿出来,我就会开始遇到问题.
I am using backbone.js. I have added require.js to include jquery at the bottom of the page. Also, I need to add jquery at the start of the page. Once I add these two, then my whole page renders properly. If I take any of them out I start getting issues.
有人能解释一下为什么会发生这个问题吗.
Can someone throw some light on why this issue is happening.
在页面的HEAD
标签中添加的脚本:
Script added in the HEAD
tag of page:
<script src="../vendors/jquery/dist/jquery.min.js"></script>
在BODY
底部添加的脚本:
Script added in BODY
bottom:
<script type="text/javascript">
require(['common'],function()
{
require(['jquery', 'fastclick','nprogress','charts','underscore'],function()
{
require(['firstDashboardController']);
});
});
</script>
如果我使用包含在下面页面顶部的 jquery 是我得到的错误:
If I take the jquery included at the top of the page below is the error I get:
如果我从底部的 require 模块中取出 jquery,则错误如下:
If I take the jquery out of require module at the bottom, then below is the error:
P.S:我使用chart.js 一个外部文件在页面上绘制图形和内容.
P.S: I am using chart.js an external file to draw graph and stuff on the page.
推荐答案
这个问题的一个常见解决方案是使用 jQuery 的内置 $.noConflict(true)
但我建议你在您的底部 标记代码中使用以下内容以防止 jQuery 的双重加载:
A common solution to this problem would be to use jQuery's built-in $.noConflict(true)
but I would suggest you to use the following in your bottom <script>
tag code to prevent the double loading of jQuery altogether:
requirejs.config({
paths: {
jquery: '../vendors/jquery/dist/jquery.min.js'
}
});
if (typeof jQuery === 'function') {
//jQuery already loaded, just use that
define('jquery', function() { return jQuery; });
}
require(["jquery"], function($) {
//This $ should be from the first jquery tag, and no
//double load.
});
来源:https://github.com/requirejs/requirejs/issues/535
去掉一个包含是行不通的,因为:
Getting rid of one include won't work because:
- 您需要在 Bootstrap 之前加载 jQuery(第一个
标签)
- 您也使用RequireJS加载的模块不能直接使用jQuery,即通过
<script src="jQuery.min.js">
标签(底部标签)加载立>
- You need jQuery to be loaded before Bootstrap (1st
<script>
tag) - The module's you also loaded using RequireJS can't directly use jQuery, that is loaded via a
<script src="jQuery.min.js">
tag (bottom tag)
这篇关于为什么我需要两次加载 jquery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!