简体   繁体   English

jQuery插件在WordPress中不起作用

[英]jQuery plugin doesn't work in WordPress

I'm trying to implement a jQuery Plugin to my wordpress site. 我正在尝试将jQuery插件实现到我的wordpress网站。 I already figured out how to make jQuery run, but the plugin still doesn't work. 我已经弄清楚了如何使jQuery运行,但是该插件仍然无法正常工作。 It is this plugin: contenthover 就是这个插件: contenthover

I tried it like this: 我这样尝试过:

<script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
<script src="http://example.com/wp-content/themes/xenia/assets/js/jquery.contenthover.min.js"></script>
<script type="text/javascript">
    <!--//-->
    <![CDATA[//>
    <!--
        jQuery(document).ready(function() {
            $('#d1').contenthover({
                overlay_background:'#000',
                overlay_opacity:0.8
            });
        });
     //-->
    <!]]//>
</script>

Whatever I try doesn't work. 无论我尝试什么都行不通。 I already included jQuery and the plugin via functions.php . 我已经通过functions.php包含了jQuery和插件。 When I call the simple slideUp(); 当我调用简单的slideUp(); it works, but the contenthover-plugin doesn't. 它有效,但contenthover-plugin无效。 What am I doing wrong? 我究竟做错了什么?

edit: This is how I also tried it by hook it up in teh functions.php of the theme 编辑:这也是我通过将其挂接到主题的functions.php中来尝试的方法

wp_register_script(THEME_SLUG . '-jqueryMain', THEME_URI . '/assets/js/jquery-2.1.4.min.js', array('jquery'));
wp_register_script(THEME_SLUG . '-contenthover', THEME_URI . '/assets/js/jquery.contenthover.min.js', array('jquery'));

wp_enqueue_script(THEME_SLUG . '-jqueryMain');
wp_enqueue_script(THEME_SLUG . '-contenthover');

WordPress uses a noconflict wrapper because other libraries also use $ as the core variable, so $ is not directly available in WordPress. WordPress使用noconflict包装器,因为其他库也将$用作核心变量,因此$在WordPress中不直接可用。

If you still want to use $ in your script, you have to define it like this: 如果仍要在脚本中使用$ ,则必须按以下方式定义它:

jQuery(document).ready(function($) {
//                              ^-- add the dollar sign here

Inside this function $ will work as expected. 在此函数内, $将按预期工作。

You can read more about it in the documentation . 您可以在文档中阅读有关它的更多信息。

Note: You should avoid adding jQuery yourself, other plugins or themes might rely on the jQuery version provided by WordPress. 注意:您应该避免自己添加jQuery,否则其他插件或主题可能会依赖WordPress提供的jQuery版本。 Just enqueue the jQuery WordPress provides. 只需排队WordPress WordPress提供的jQuery。

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

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