jQuery:$.getScript无效的疑问

现需要动态加载EasyUI组件,因为我的页面内嵌于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);

现想将其改为如下形式可正确加载,但组件未生效

$.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呢?

阅读 2.6k
1 个回答

你第一种的方式parent.document这个是在父页面进行的操作。
而第二种则是在当前页面进行的操作,肯定不一样了。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题