如何解决“TypeError:$不是函数” WordPress自定义页面错误? [英] How to fix "TypeError: $ is not a function" error in WordPress custom page?
问题描述
我通过插件创建了自定义WordPress页面,我希望使用此代码打开/关闭评论
I created custom WordPress page via plugin where I want to toggle on/off comments using this code
<script type="text/javascript">
$("comment_switch").click(function () {
$("comments").toggleClass("hidden");
});
</script>
我将它放在< body>
标签。要生成< head>
标签,我使用了标准的WordPress函数 wp_head();
。当我检查页面的源代码时,我可以在头部分中看到< script src =http://10.1.1.6/wp-includes/js/jquery/jquery.js?ver=1.10 .2type =text / javascript>
我认为这足以使用jQuery。
I placed it inside the <body>
tag. To generate <head>
tag I used standard WordPress function wp_head();
. When I check the source code of the page I can see in head section <script src="http://10.1.1.6/wp-includes/js/jquery/jquery.js?ver=1.10.2" type="text/javascript">
which I thought would be enough to use jQuery.
有人可以帮我做jQuery代码的工作?该页面的整个源代码可以在这里找到。
Could someone help me to make the jQuery code work? The whole source code of the page could be found here
推荐答案
您可能缺少一些。
类标记和 DOM就绪函数
You're probably missing some .
class markup and the DOM ready function
jQuery(function($) { // DOM is now ready and jQuery's $ alias sandboxed
$(".comment_switch").click(function () {
$(".comments").toggleClass("hidden");
});
});
这篇关于如何解决“TypeError:$不是函数” WordPress自定义页面错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!