[英]Wordpress plugin doesn't load jQuery
I'm building a FAQ plugin for my website in Wordpress but the jQuery doesn't load. 我正在Wordpress中为我的网站构建FAQ插件,但jQuery无法加载。 I had the same problem when I tried to hide a submit button on a contact form earlier and had to hide it with CSS.
当我尝试早些时候在联系人表单上隐藏提交按钮并不得不用CSS将其隐藏时,我遇到了同样的问题。 Should also say i'm new to jQuery and it's possible I have missed something.
还应该说我是jQuery新手,可能我错过了一些东西。 Thanks!
谢谢!
PHP: PHP:
wp_register_script( 'javascript', plugins_url('js/javascript.js',__FILE__ ));
wp_enqueue_script('javascript');
echo '<div class="qaleft">' . '<div class="toggle">' . '<h3>' . $post_titles . '</h3>' . '<div class="toggle-info">' . '<p>' . $post_content . '</p>' . '</div>' . '</div>' . '</div>';
jQuery: jQuery的:
$(function(){
$('.toggle h3').on('click', function(e){
var answer = $(this).next('.toggle-info');
if(!$(answer).is(":visible")) {
$(this).parent().addClass('open');
} else {
$(this).parent().removeClass('open');
}
$(answer).slideToggle(300);
});
});
In Wordpress jQuery uses no conflict mode, so you cannot use the dollar sign as you are attempting. 在Wordpress中,jQuery不使用冲突模式,因此您无法在尝试时使用美元符号。
To fix use the full name and pass the dollar as a parameter: 要解决此问题,请使用全名并将美元作为参数传递:
jQuery(function($){
//from here on you can use $ as a placeholder to jQuery, as you normally do
$('.toggle h3').on('click', function(e){
var answer = $(this).next('.toggle-info');
if(!$(answer).is(":visible")) {
$(this).parent().addClass('open');
} else {
$(this).parent().removeClass('open');
}
$(answer).slideToggle(300);
});
});
Also, make sure jquery is loaded by including it in the dependancy argument when you register your custom script: 另外,通过在注册自定义脚本时将jquery包含在dependency参数中来确保jquery已加载:
wp_register_script( 'javascript', plugins_url('js/javascript.js',__FILE__ ), array( 'jquery' ));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.