简体   繁体   English

未捕获的 ReferenceError:jQuery 未在 wordpress 自定义脚本中定义

[英]Uncaught ReferenceError: jQuery is not defined in wordpress custom script

My jquery file is not working in WordPress.我的 jquery 文件在 WordPress 中不起作用。

The error is here:错误在这里:

在此处输入图片说明

My js code:我的js代码:

(function($) {
    $(document).ready(function(){
    $('#read_more').click(function() {
    $('.display_none').show().animate({'transition': '2s'});
   });
  });
})( jQuery );

I have an added custom-script.js file in functions.php like this:我在functions.php添加了一个custom-script.js文件,如下所示:

function aristo_css_js(){
  wp_enqueue_script('myjs', get_template_directory_uri().'/assets/js/custom-script.js');
  wp_enqueue_script('myjs');
}
add_action('wp_enqueue_scripts','aristo_css_js');

What is the main problem for this was not working?这不起作用的主要问题是什么? But if I added custom script code before body tag it's working.但是如果我在body标记之前添加了自定义脚本代码,它就可以工作。 ? ?

I solve my own question by add jquery file before my custom-script我通过在custom-script之前添加jquery文件来解决我自己的问题

wp_enqueue_script('jquery-main', get_template_directory_uri().'/assets/js/jquery-3.3.1.min.js');
wp_enqueue_script('myjs', get_template_directory_uri().'/assets/js/custom-script.js');

I hope this will fix your issue我希望这能解决你的问题

(function($) {

// Use $() inside of this function like so 

$(#selector)

})(jQuery);

I had this same issue once.我曾经遇到过同样的问题。

WordPress ships with its own version of the jQuery library. WordPress 附带了自己版本的 jQuery 库。

Try using jQuery instead of just $ sign.尝试使用jQuery而不是$符号。

For example:例如:

var svg = $("#svg-container");

should be replaced with应该替换为

var svg = jQuery("#svg-container");

Try to use this updated javascript code,尝试使用这个更新的 javascript 代码,

 jQuery.noConflict(); (function(jQuery) { jQuery(document).ready(function(){ jQuery('#read_more').click(function() { jQuery('.display_none').show().animate({'transition': '2s'}); }); }); })( jQuery );

 function aristo_css_js(){ wp_enqueue_script('jquery-main', get_template_directory_uri().'/assets/js/jquery-3.3.1.min.js'); wp_enqueue_script('myjs', get_template_directory_uri().'/assets/js/custom-script.js'); } add_action('wp_enqueue_scripts','aristo_css_js');

Thanks!谢谢!

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

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