且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

为什么我需要两次加载 jquery

更新时间:2023-02-14 18:15:01

这个问题的一个常见解决方案是使用 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:

  1. 您需要在 Bootstrap 之前加载 jQuery(第一个 标签)
  2. 您也使用RequireJS加载的模块不能直接使用jQuery,即通过<script src="jQuery.min.js">标签(底部标签)加载立>
  1. You need jQuery to be loaded before Bootstrap (1st <script> tag)
  2. 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)