簡體   English   中英

在外部javascript文件中加載jquery

[英]load jquery in external javascript file

我想在外部js文件中加載jquery並使用它。 正在以下面的方式在test.js中進行操作

   var src='http://code.jquery.com/jquery-1.10.1.min.js';
   document.write('<sc'+'ript type="text/javascript" src="'+src+'" onload="init()">     </sc'+'ript>');

 function  init(){
       $(function(){
         //jquery code here
  });
 }

如果不調用函數init() onload,則會收到$引用錯誤。 還有其他更好的方法,然后建議我。 謝謝

嘗試改用getScript

http://api.jquery.com/jQuery.getScript/

$.getScript( "external.js" )
  .done(function( script, textStatus ) {
    init();
  })
  .fail(function( jqxhr, settings, exception ) {
    console.log("Triggered ajaxError handler.");
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM