简体   繁体   English

jQuery加载动态库

[英]Jquery loading dynamic libraries

I am making a script where my jquery loads a jquery plugin with the getScript() method. 我正在制作一个脚本,其中我的jquery使用getScript()方法加载了jquery插件。 But for some reason the code that I load only works after my script has ended. 但是由于某种原因,我加载的代码仅在脚本结束后才能运行。

So the procedure: 所以程序:

  1. load jquery 加载jQuery
  2. do the getScript() method and load the plugin. 执行getScript()方法并加载插件。
  3. try to use the plugin ($('#test').myPlugin()) but crashes. 尝试使用插件($('#test')。myPlugin()),但崩溃。
  4. script ended. 脚本结束。 try $('#test').myPlugin() in chrome web developer. 在Chrome Web开发人员中尝试$('#test')。myPlugin()。 Works perfectly. 完美运作。

The getScript()-method retrieves the script asynchronous, so you have to wait till it has loaded (usually through the success()-method) to call it. getScript()方法以异步方式检索脚本,因此您必须等待脚本加载完成(通常是通过success()方法)来调用它。 I guess you're not doing that. 我想你不是那样做的。

So try 所以尝试

getScript("script").success(function() {
    $('#test').myPlugin();
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM