jquery - 如何通过小部件代码使用网站的 jQuery

标签 jquery jquery-ui jquery-plugins widget transclusion

我有一个小部件,可以添加到随机网站。

每当这些网站有 jQuery 时,我希望我的代码使用它,而不是再次上传 jQuery。

如何通过一次性设置,使我对 jQuery 的函数调用调用站点的 jQuery,可能使用不同的 jQuery 前缀 ($)?

最佳答案

使用匿名函数来测试 jQuery 对象是否存在。如果未定义,请将其添加到 DOM 并短暂等待,然后重试:

void(function()
  {
  if (/function/.test(this["jQuery"]) == false)
    {
    var anon = arguments.callee;
    var jqs = document.createElement("script");
    jqs.src = "http://code.jquery.com/jquery-latest.js";
    document.getElementsByTagName("body")[0].appendChild(jqs);

    window.setTimeout(function () {anon();}, 500);
    }
    else
      {
      $(document).ready(function(){document.title = "jQuery"});
      }
  }
());

关于jquery - 如何通过小部件代码使用网站的 jQuery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6134499/

相关文章:

javascript - MVC3::从详细信息 View 中导航表,而不是单击表中的详细信息链接

javascript - JQuery Draggable 以编程方式拖动到位置

jquery-plugins - OwlCarousel 1.3.2 无限循环

jquery - Colorbox 使灯箱在滚动时固定

jquery - 有人知道一个很酷的 jquery 进化图片吗?

jQuery IE8 - Firefox 问题

javascript - 在 thymeleaf th :src 中使用 javascript

jQuery 模式对话框和 CKEditor 输入对话框

jquery - 固定 div 位置而不考虑滚动条

javascript - 函数应该在 jQuery 的扩展内部还是外部?