【问题标题】:How to include jQuery library in Javascript without <script src="">如何在没有 <script src=""> 的情况下在 Javascript 中包含 jQuery 库
【发布时间】:2014-12-06 13:46:24
【问题描述】:

我需要在 javascript 文件 (john.js) 中远程包含 jQuery 库。我试过这个没有任何运气;

(function(d, t) {
    var g = d.createElement(t), // create a script tag
        s = d.getElementsByTagName(t)[0]; // find the first script tag in the document
    g.src = 'http://code.jquery.com/jquery-latest.js'; // set the source of the script to your script
    s.parentNode.insertBefore(g, s); // append the script to the DOM
}(document, 'script'));

$( document ).ready(function() {

// My jquery works here

});

我想以 javascript 方式获取脚本。这样做的正确方法是什么?

【问题讨论】:

  • 你看过 jQuery 的 getScript() 函数吗? api.jquery.com/jquery.getscript
  • 我想获取 Jquery,而不是 js。以及如何使用,当你还没有包含 jquery 库时? getScript() 是一个 jQuery 函数,而不是 javascript。
  • 但是您是否看过 getScript() 的代码以了解其工作原理?
  • 我认识那个脚本伙伴。但正如我所说,它的 JQUERY 函数。我需要用javascript方式制作它。
  • jQuery 100% 是用 JavaScript 编写的,所以很明显 jQuery 能做的任何事情都可以用纯 JavaScript 来实现。所以,再一次,如果你检查 getScript() 的 jQuery 源代码,你可以看到它是如何工作的,并自己包含一些版本。

标签: javascript jquery


【解决方案1】:

这里的错误是这段代码执行时JQuery还没有加载:

$(document).ready(function() { .. }

按原样,您会收到如下错误:$ is undefined(或类似)

您应该使用创建脚本元素的onload 事件来确保它已加载 Jquery。

此示例展示了您如何实现目标。祝你好运。

var newScript = document.createElement('script');
newScript.type = 'text/javascript';
newScript.src = 'http://code.jquery.com/jquery-latest.js'; // set the source of the script to your script
newScript.onload = function() {
  alert("Script is ready!");
  $(document).ready(function() {
    alert("JQuery is ready!");
  });
};
var head = document.getElementsByTagName("head")[0];
head.appendChild(newScript);

【讨论】:

    【解决方案2】:

    这是关于如何获取的答案,来自另一篇帖子:

    Adding <script> element to the DOM and have the javascript run?

    蜜蜂说你可能遇到的另一个问题是,当你打电话给你的

    $( document ).ready(function() {
    
    // My jquery works here
    
    });
    

    Jquery 可能还没有加载

    所以你可以尝试一个递归函数来检查 jquery 是否已经加载了类似的东西(不是 teste)......

    function launch(callBack){
        if (window.jQuery) {  
           callBack();
        } else {
            setTimeout(function(){launch(callBack);},100);
        }
    }
    launch(function(){
       $( document ).ready(function() {
         // My jquery works here
       });
    });
    

    【讨论】:

      【解决方案3】:

      您可以使用XMLHttpRequesteval 的组合来动态获取javascript 文件,就像getScript 对jQuery 所做的那样。

      见我的fiddle

      【讨论】:

      • 不,我想获取 Jquery,而不是 js。 getScript 是 jquery 函数,当你没有包含 jQuery 库时它不能工作
      • 据我所知,xmlhttprequest 向网页发送请求,如何获取脚本。可以给我看一个简单的例子吗?
      • 看我的小提琴。如果它回答了您的问题,请不要忘记接受和/或支持我的回答!我希望得到一些积分。
      • 完全不用eval来做这个
      【解决方案4】:

      通常在调用自定义 javascript 文件之前在 html 文件中调用 jquery:

      <script src="http://code.jquery.com/jquery-latest.js"></script>
      <script src="app.js"></script>
      

      【讨论】:

        猜你喜欢
        • 2022-11-22
        • 1970-01-01
        • 2012-04-01
        • 2018-10-01
        • 1970-01-01
        • 2020-06-09
        • 2011-10-11
        • 2023-03-16
        • 1970-01-01
        相关资源
        最近更新 更多