如何修复“TypeError: $ is not a function"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>
我把它放在 标签内.为了生成
标签,我使用了标准的 WordPress 函数
wp_head();
.当我检查页面的源代码时,我可以在头部部分看到 <script src="http://10.1.1.6/wp-includes/js/jquery/jquery.js?ver=1.10.2"type="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: $ is not a function"WordPress 自定义页面中的错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!