TypeError:$不是jQuery函数中的函数 [英] TypeError: $ is not a function at jQuery function
本文介绍了TypeError:$不是jQuery函数中的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在使用jQuery包装器的WordPress插件中有一个简单的jQuery脚本:
I have a simple jQuery script in a WordPress plugin that is using a jQuery wrapper:
$("#myForm").submit(function(e){
e.preventDefault();
if ($('input[name="username"]').val() == "" || $('input[name="password"]').val() == "")
$("#NotesArea").html("Please enter both Username and Passnumber");
else
$.post($(this).attr("action"), $(this).serialize())
.done(function(data) { $("#NotesArea").html(data); })
.fail(function(jqXHR, textStatus) {alert( "Request failed: " + textStatus );});
});
我从WordPress仪表板中调用此脚本,并且总是收到此错误:
I am calling this script from within the WordPress Dashboard and i am getting always this error:
未捕获的TypeError:$不是函数
Uncaught TypeError: $ is not a function
好的,我是这样的,没有错误,但是代码不起作用!
Alright, I made it like this, no errors but the code doesn't work!
(function($){
$("#myForm").submit(function(e){
e.preventDefault();
if ($('input[name="username"]').val() == "" || $('input[name="password"]').val() == "")
$("#NotesArea").html("Please enter both Username and Passnumber");
else
$.post($(this).attr("action"), $(this).serialize())
.done(function(data) { $("#NotesArea").html(data); })
.fail(function(jqXHR, textStatus) {alert( "Request failed: " + textStatus );});
});
})(jQuery);
推荐答案
WordPress使用noConflict删除$
别名
Wordpress use noConflict which removes the $
alias
在下面的IIFE中包装您的代码,您可以继续在其中使用$
Wrap your code in the following IIFE and you can continue using $
inside it
(function($){
/* your code*/
})(jQuery);
这篇关于TypeError:$不是jQuery函数中的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文