简体   繁体   English

Wordpress:Javascript已“加载”,但无法使用控制台错误

[英]Wordpress: Javascript 'loaded' but not working with console errors

I'm just familiarising myself with Wordpress and enqueuing scripts so I'd appreciate some help with this. 我只是熟悉Wordpress和使脚本入队,因此希望对此有所帮助。 I'll try and give as much detail as possible. 我将尝试并提供尽可能多的细节。

It's worth nothing that all this scripts and how they're loaded work fine in my html templates but now they're in Wordpress they don't seem to load/I get console errors which aren't there in the static templates. 所有这些脚本及其加载方式在我的html模板中都可以正常工作,这毫无价值,但是现在它们在Wordpress中似乎无法加载/我收到静态模板中没有的控制台错误。

In my static templates I load the following files: 在我的静态模板中,我加载以下文件:

<script src="js/jquery-1.12.0.min.js"></script>
<script src="js/responsive-nav.js"></script>
<script src="js/jquery.uniform.min.js"></script>
<script src="js/main.js"></script>

In main.js I load the 2 scripts that preceed it with the following: main.js我加载了以下脚本之前的2个脚本:

/**
 * RESPONSIVE-NAV.JS (plug-in)
 */ 

$(function(){

    var navigation = responsiveNav(".site-nav__list", {
        customToggle: "#site-nav__toggle",
        open: function(){
            $("#site-nav__toggle").addClass('open');
        },
        close: function(){
            $("#site-nav__toggle").removeClass('open');
        }
    });
});

/**
 * UNIFORM.JS (plug-in)
 */ 

$("select, input[type='file'], input[type='checkbox'], input[type='radio']").uniform({selectAutoWidth: false, fileButtonClass: 'btn'});

The scripts don't load and I get these console errors: 脚本无法加载,并且出现以下控制台错误:

Uncaught TypeError: $ is not a function
    at main.js?ver=4.6.4:10
(anonymous) @ main.js?ver=4.6.4:10

(index):46 Uncaught ReferenceError: conditionizr is not defined
    at (index):46

And my scripts are queued like this: 我的脚本像这样排队:

add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_styles', 100 );
function my_theme_enqueue_styles() {

    // Dequeue files
    wp_dequeue_style( 'normalize');
    wp_dequeue_style( 'html5blank');

    // Equeue files
    wp_enqueue_style( 'main-css', get_stylesheet_directory_uri() . '/css/main.css' );

    wp_enqueue_style( 'google-fonts', 'https://fonts.googleapis.com/css?family=Montserrat:400,700|Open+Sans:300,300i,400,400i,600,600i,700,700i');
}

add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_scripts', 100 );
function my_theme_enqueue_scripts() {

    // Dequeue files
    wp_deregister_script( 'conditionizr');
    wp_deregister_script( 'modernizr');
    wp_deregister_script( 'html5blankscripts');

    // Register/equeue files
    wp_register_script('responsive-nav', get_stylesheet_directory_uri() . '/js/responsive-nav.js', array('jquery'));
    wp_enqueue_script('responsive-nav'); // Enqueue it!

    wp_register_script('uniform-js', get_stylesheet_directory_uri() . '/js/jquery.uniform.min.js', array('jquery'));
    wp_enqueue_script('uniform-js');

    wp_register_script('main-js', get_stylesheet_directory_uri() . '/js/main.js', array('jquery'));
    wp_enqueue_script('main-js');
}

If I remove the wp_deregister_script( 'conditionizr'); 如果我删除了wp_deregister_script( 'conditionizr'); that seems to get rid of one of the errors but it's not a script I need - I'm using a child-theme so I just wanted to strip any of the parent themes css/scripts from the markup and use my own. 似乎摆脱了一个错误,但这不是我需要的脚本-我使用的是儿童主题,因此我只想从标记中剥离任何父主题CSS /脚本并使用我自己的主题。

Hope someone can help with this! 希望有人可以帮忙!

If it helps, here's the parent themes enqueued styles also: 如果有帮助,这里还列出了父主题样式:

// Load HTML5 Blank scripts (header.php)
function html5blank_header_scripts()
{
    if ($GLOBALS['pagenow'] != 'wp-login.php' && !is_admin()) {

        wp_register_script('conditionizr', get_template_directory_uri() . '/js/lib/conditionizr-4.3.0.min.js', array(), '4.3.0'); // Conditionizr
        wp_enqueue_script('conditionizr'); // Enqueue it!

        wp_register_script('modernizr', get_template_directory_uri() . '/js/lib/modernizr-2.7.1.min.js', array(), '2.7.1'); // Modernizr
        wp_enqueue_script('modernizr'); // Enqueue it!

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

// Load HTML5 Blank conditional scripts
function html5blank_conditional_scripts()
{
    if (is_page('pagenamehere')) {
        wp_register_script('scriptname', get_template_directory_uri() . '/js/scriptname.js', array('jquery'), '1.0.0'); // Conditional script(s)
        wp_enqueue_script('scriptname'); // Enqueue it!
    }
}

// Load HTML5 Blank styles
function html5blank_styles()
{
    wp_register_style('normalize', get_template_directory_uri() . '/normalize.css', array(), '1.0', 'all');
    wp_enqueue_style('normalize'); // Enqueue it!

    wp_register_style('html5blank', get_template_directory_uri() . '/style.css', array(), '1.0', 'all');
    wp_enqueue_style('html5blank'); // Enqueue it!
}

As others have pointed out, you need to fix your dependency array in your wp_register_script() statements. 正如其他人指出的那样,您需要在wp_register_script()语句中修复依赖项数组。

On a side note, you do not need to use both wp_register_script() and wp_enqueue_script() . 附带说明,您无需同时使用wp_register_script()wp_enqueue_script() Just follow the wp_register_script() syntax and you're fine. 只需遵循wp_register_script()语法,就可以了。 For example: 例如:

wp_enqueue_script( 'my-script', get_stylesheet_directory_uri() . /js/script.js', array( 'jquery', 'responsive-nav', 'uniform-js', '', true);

This will register the handle 'my-script', associate it with the script.j s file in your theme directory's /js directory, tell WordPress it needs jQuery , 'responsive-nav' , and 'uniform-js' in order to run, there's no version number, and to load it in the footer. 这将注册句柄“ my-script”,并将其与主题目录的/js目录中的script.j s文件相关联,告诉WordPress它需要jQuery'responsive-nav''uniform-js'才能运行,没有版本号,并将其加载到页脚中。 All dependencies must be loaded prior. 必须先加载所有依赖项。 jQuery is the only one that comes with WordPress so the others will need to be enqueued first. jQuery是WordPress随附的唯一工具,因此其他工具需要首先入队。

As for jQuery, WordPress loads it in no-conflict mode so you will NOT have access to $() by default. 至于jQuery,WordPress将其以无冲突模式加载,因此默认情况下您将无权访问$() There are a few work-arounds for this. 有一些解决方法。 The easiest is to simply replace $() with jQuery() . 最简单的方法是将$()替换$() jQuery() If your script has to load in the header, then this is what you want to do. 如果您的脚本必须加载到标题中,那么这就是您想要做的。

If it can load in the footer, which is the option you want, then you can wrap your entire script in an anonymous function passing jQuery in. So it would look like this: 如果它可以加载到页脚中(这是您想要的选项),则可以将整个脚本包装在传递jQuery的匿名函数中。因此,它看起来像这样:

( function( $ ) {
   // Your jQuery Script Here
} )( jQuery );

Also, instead of just using wp_deregister_script() , try using just wp_dequeue_script() instead. 此外,而不是只用wp_deregister_script()请尝试使用刚刚wp_dequeue_script()来代替。 So: 所以:

wp_dequeue_script( 'modernizr' );

I won't pretend to know much about those Wordpress internal functions, but your first error indicates that something is using the conditionizr object. 我不会假装对这些Wordpress内部函数了解太多,但是您的第一个错误表明某些东西正在使用conditionizr对象。 You should probably not be using wp_deregister_script('conditionizr') unless you deal with the script file referencing it first. 除非您先处理引用它的脚本文件,否则您可能不应该使用wp_deregister_script('conditionizr')

As to the other error, I did have to add some jQuery script to a WP site recently, and was having trouble using $ . 至于另一个错误,最近我确实必须向WP网站添加一些jQuery脚本,并且在使用$遇到了麻烦。 Replacing it with jQuery solved that issue. jQuery替换它可以解决该问题。 For example: 例如:

jQuery(function(){

    var navigation = responsiveNav(".site-nav__list", {
        customToggle: "#site-nav__toggle",
        open: function(){
            jQuery("#site-nav__toggle").addClass('open');
        },
        close: function(){
            jQuery("#site-nav__toggle").removeClass('open');
        }
    });
});

jQuery("select, input[type='file'], input[type='checkbox'], input[type='radio']").uniform({selectAutoWidth: false, fileButtonClass: 'btn'});

According to a comment below, this is because jQuery has a no-conflict mode which will "relinquish control of the $ variable." 根据下面的评论,这是因为jQuery具有无冲突模式 ,该模式将“放弃对$变量的控制”。

• You need to include both responsive-nav and uniform-js in the dependancies of main-js , because you are making use of them inside main.js. •您需要在main-js的依赖关系中同时包含响应式导航统一 js ,因为您要在main.js中使用它们。 (Actually, as you are calling jQuery as a dependency in each of those scripts there is no need to call it again) (实际上,由于您在每个脚本中都将jQuery称为依赖项,因此无需再次调用它)

wp_register_script('main-js', get_stylesheet_directory_uri() . '/js/main.js', array('jquery','responsive-nav','uniform-js'));
wp_enqueue_script('main-js');

• Error 1. Instead of starting your function : •错误1. 而不是启动您的功能

$(function(){

try: 尝试:

jQuery(function($){

• Error 2. It seems like some of the scripts is calling conditionizr function so, as you are deregistering, it JavaScript throws an error. •错误2。似乎某些脚本正在调用conditionizr函数,因此在注销时,JavaScript会引发错误。 Find out which script is calling it and remove that function call. 找出正在调用哪个脚本并删除该函数调用。 (I might be wrong but it looks like it is being called from an inline script, check for wp_add_inline_script ) (我可能是错的,但看起来好像是从内联脚本调用的,请检查wp_add_inline_script

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

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