使用appendchild有条件地加载jQuery

时间:2011-06-16 12:44:53

标签: jquery wordpress plugins colorbox appendchild

我有一个wordpress插件来生成一个flickr库。为了避免与其他jquery插件冲突,我只加载jquery(如果它尚未加载)。但是这种方法的问题是,如果使用此方法加载jquery,则colorbox不起作用。我没有在页面正文中看到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);
}

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

1 个答案:

答案 0 :(得分:2)

您应该使用enqueue_script函数。

“将javascripts添加到WordPress生成的页面的安全方法。基本上,如果脚本尚未包含,请包含该脚本,并加载WordPress发布的脚本。”

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

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

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