【问题标题】:Working with jQuery in HTML5 Blank Wordpress theme在 HTML5 空白 Wordpress 主题中使用 jQuery
【发布时间】:2014-08-20 10:50:14
【问题描述】:

我在尝试解决这个问题时遇到了大问题。我擅长HTMLCSS,但我总是很难使用JSjQuery,当它进入Wordpress 主题时更是如此。

所以我希望有人可以指导我完成这个。 HTML5Blank 主题通过 functions.php 加载几个 JS 文件,利用接下来的几行来加载自定义脚本的文件:

wp_register_script('html5blankscripts', get_template_directory_uri() . '/js/scripts.js', array('jquery'), '1.0.0'); // Custom scripts
wp_enqueue_script('html5blankscripts'); // Enqueue it!

那个文件的内容是:

(function ($, root, undefined) {

    $(function () {

        'use strict';

        // DOM ready, take it away

    });

})(jQuery, this);

那么现在......我应该怎么做才能在上述文件中正确插入下一个 jQuery 代码?

$(".abc").mouseenter(function(){
    clearTimeout($('#def').data('timeoutId'));
    $('#def').show(200);
}).mouseleave(function(){
    var someElement = $(this);
    var timeoutId = setTimeout(function(){
        $('#def').hide(200);
    }, 650);
    $('#def').data('timeoutId', timeoutId); 
});

$("#def").mouseenter(function(){
    clearTimeout($('#def').data('timeoutId'));
}).mouseleave(function(){
    var someElement = $(this);
    var timeoutId = setTimeout(function(){
        $('#def').hide(200);
    }, 650);
    $('#def').data('timeoutId', timeoutId); 
});

我在这件事上很迷茫,所以你能给我的任何帮助将不胜感激:)

【问题讨论】:

    标签: jquery wordpress wordpress-theming


    【解决方案1】:

    下面的代码应该可以做到:

    (function ($, root, undefined) {
    
        $(function () {
    
            'use strict';
    
            // DOM ready, take it away
            $(".abc").mouseenter(function () {
                clearTimeout($('#def').data('timeoutId'));
                $('#def').show(200);
            }).mouseleave(function () {
                var someElement = $(this);
                var timeoutId = setTimeout(function () {
                    $('#def').hide(200);
                }, 650);
                $('#def').data('timeoutId', timeoutId);
            });
    
            $("#def").mouseenter(function () {
                clearTimeout($('#def').data('timeoutId'));
            }).mouseleave(function () {
                var someElement = $(this);
                var timeoutId = setTimeout(function () {
                    $('#def').hide(200);
                }, 650);
                $('#def').data('timeoutId', timeoutId);
            });
    
        });
    
    })(jQuery, this);
    

    JSFiddle

    【讨论】:

    • 效果很好!非常感谢这么快的回复:) 想问你点别的。如果我想在这个文件中添加多个函数,我应该怎么做?哦,还有……(function ($, root, undefined) 代码行有什么作用?
    • @RodniD Wordpress 在 no-conflict 模式下实现 jQuery,以免与可能使用 $ 符号作为命名空间的其他库发生潜在冲突,因此只能通过调用 @987654327 来引用它@ 但是使用该功能,您实际上是将jQuery 作为参考传递,将this 作为root 您不需要在那里的undefined AFAIK,digwp.com/2011/09/using-instead-of-jquery-in-wordpress 至于添加其他功能,您可以这样做您通常会这样做,但它们不会在 window 对象中限定范围,因此只能从代码块中的代码访问。
    • 这当然可以说明问题:) 但是我怎样才能正确地在上面的代码中添加新功能,@RobSchmuecker?
    • @RodniD 如果需要引用 jquery,您可以将代码放在匿名函数内部,如果不需要,可以将代码放在外部。外部定义的那些函数可以在window 范围内访问。
    • 想我现在明白了。非常感谢@RobSchmuecker! :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-14
    • 1970-01-01
    • 2014-04-20
    • 1970-01-01
    相关资源
    最近更新 更多