【问题标题】:How to implement jQuery-Plugin to Wordpress如何实现 jQuery 插件到 Wordpress
【发布时间】:2015-11-11 14:33:04
【问题描述】:

我仍在尝试在 wordpress 中实现 jquery.contenthover.js-插件,但我无法使其工作。

首先,我注销了 wordpress 加载的 jQuery,因为该插件需要更高版本,并注册并入队更高版本.. 然后将 plugin-js 入队.. 为此,我在我的 functions.php 中添加了以下内容

function init_js_scripts() {
    if (!is_admin()) {

        wp_deregister_script('jquery');
        wp_register_script('jquery','//ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js',false, '2.1.4');
        wp_enqueue_script('jquery');                        

        wp_enqueue_script('contenthover', get_template_directory_uri() . '/assets/js/jquery.contenthover.min.js', array('jquery'), false, true);


    }
}

add_action( 'init', 'init_js_scripts');

无论我尝试什么,它都不会说:/ .. 在 Firefox-Debugger 中,新的 jquery 已正确加载,但 jquery.contenthover.min.js 显示为灰色(?)。 . 我该如何解决?

编辑:

感谢 Jeremy,我现在知道,Plugin-Js 已正确加载.. 但它仍然无法正常工作.. 当我将代码提取到新的 HTML(远离 wordpress)中时,一切正常..在 WP 中它没有.. .. 我使用

<script type="text/javascript"><!--//--><![CDATA[//><!--
var $j = jQuery.noConflict();
$j(document).ready(function () {

$j('#d1').contenthover({
    effect:'slide',
    slide_speed:300,
    overlay_background:'#000',
    overlay_opacity:0.8
});
});
 //--><!]]//></script>

但毫不费力。谁能告诉我,我做错了什么?

【问题讨论】:

  • 您的页面上的script 标记在哪里?它必须出现在包含其他脚本之后。为了确保确实如此,您应该在外部文件中编写此脚本并将其加入队列,就像您将库加入队列一样(确保将 contenthover 指定为依赖项)。
  • jQuery-2.1.4 位于标题中,因为已加载的第一个 js .. contenthover.js 和 contenthoverInit(这里我放了 DIV 的初始化代码)位于源代码的底部..靠近

标签: javascript php jquery wordpress plugins


【解决方案1】:

Firefox 以缩小的灰色脚本显示,就像您包含的那样。所以实际上,没有问题:您的脚本已包含在内。

但是,您应该避免使用 init 操作来使脚本排队,因为 wp_enqueue_scripts 在这里是一个更好的操作。此外,您指出新的 jQuery 是您的脚本的依赖项,因此您不需要自己将其加入队列。如果您想了解更多信息,我写了a complete guide about wp_enqueue_script()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-15
    • 1970-01-01
    • 2012-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多