JavaScript WordPress未捕获的typeerror $不是函数

JavaScript WordPress未捕获的typeerror $不是函数

问题描述:

$(document).ready(function(){
   $(".showhide").click(function(){
      $("nav").slideToggle("slow");
   });
});

我不是JavaScript程序员.我收到此错误:Uncaught TypeError $ is not a function

I'm not a JavaScript programmer. I'm getting this error: Uncaught TypeError $ is not a function

由于某种原因,我的导航无法正常工作. 有人告诉我将其放在JavaScript中,但出现错误.

For some reason my navigation is not working. Someone told me to put this in JavaScript but I'm getting the error.

发现了这个:jQuery(document);,但是不知道如何使用.

Found this: jQuery(document); but don't know how to use this.

这是您应该特别在WordPress上执行的方法(因为Wordpress通常使用jQuery而不是$)

This is how you should do it specially on WordPress (since Wordpress is usually using jQuery instead of $)

(function($){
  $(document).ready(function(){
   $(".showhide").click(function(){
      $("nav").slideToggle("slow");
   });
  });

})(jQuery);

参考此处

或者如果您不喜欢上面的答案,则可以在$(document).ready函数之前添加var $ = jQuery;

or if you don't like the answer above, you can add var $ = jQuery; before the $(document).ready function