【问题标题】:using appendchild to conditionally load jQuery使用 appendchild 有条件地加载 jQuery
【发布时间】:2011-06-16 12:44:52
【问题描述】:

我有一个 wordpress 插件来生成一个 flickr 画廊。为避免与其他 jquery 插件冲突,我仅在尚未加载 jquery 时才加载它。但是这种方法的问题是,如果使用此方法加载 jquery,则颜色框不起作用。我在页面正文中看不到 cboxoverlay 和 colorbox 元素。

这是我的代码 -

if((typeof(jQuery) == 'undefined') || (jQuery.fn.jquery < '1.4.4')){
    var loadjQuery = document.createElement(\"script\");
    loadjQuery.setAttribute(\"src\",\"https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js\");
    var head = document.getElementsByTagName(\"head\")[0];
    head.appendChild(loadjQuery);
}

有什么办法可以解决这两个问题吗?

【问题讨论】:

    标签: jquery wordpress plugins colorbox appendchild


    【解决方案1】:

    您应该使用函数 enqueue_script。

    “一种将 javascript 添加到 WordPress 生成的页面的安全方法。基本上,如果尚未包含脚本,请包含脚本,然后加载 WordPress 附带的脚本。”

    基本上,这将允许您将脚本安全地包含在 WordPress 中。

    http://codex.wordpress.org/Function_Reference/wp_enqueue_script

    如果这还不够详细,请告诉我,我可以详细说明

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-04-03
      • 1970-01-01
      • 1970-01-01
      • 2011-03-17
      • 1970-01-01
      • 1970-01-01
      • 2015-12-24
      相关资源
      最近更新 更多