鍍金池/ 問答/HTML/ jQuery:$.getScript無效的疑問

jQuery:$.getScript無效的疑問

現(xiàn)需要動態(tài)加載EasyUI組件,因為我的頁面內(nèi)嵌于iframe

通過如下方式可正確加載,且組件生效

var script = parent.document.createElement("script");
script.type = "text/javascript";
script.src = "easyUI/jquery.easyui.min.js";
parent.document.getElementsByTagName("head")[0].appendChild(script);

現(xiàn)想將其改為如下形式可正確加載,但組件未生效

$.getScript("easyUI/jquery.easyui.min.js", function( data, status, jqXHR ) {

});

顯示 $("#win").window is not a function 且原型鏈中確實沒有加載到組件

有前輩遇到過類似的問題嗎?或者我該從何排查起呢?

官方API中對該方法有如下描述

The script is executed in the global context, so it can refer to other variables and use jQuery functions. Included scripts can have some impact on the current page.

是不是因為第一種方式是加載到了iframe所在的head,而第二種方式是加載到了頂層head呢?

回答
編輯回答
萌吟

你第一種的方式parent.document這個是在父頁面進行的操作。
而第二種則是在當(dāng)前頁面進行的操作,肯定不一樣了。

2017年5月16日 02:05