我试图让这个工作很长一段时间,我仍然会遇到错误。
我正在localhost上开发一个新的Wordpress页面4.0版。 我无法让jQuery和其他脚本工作。
我了解到,我必须在functions.php中包含脚本和样式。 Jquery已经注册,所以我只能将它排队。 我还想为其他脚本添加一个额外的script.js文件。 这是我的代码
function my_scripts_styles() { wp_enqueue_script('jquery'); /* REGISTER ALL JS FOR SITE */ wp_register_script('my_scripts', get_template_directory_uri() . '/js/scripts.js'); /* REGISTER ALL CSS FOR SITE */ wp_register_style('default',get_template_directory_uri() . '/css/style.css'); /* CALL ALL CSS AND SCRIPTS FOR SITE */ wp_enqueue_script('my_scripts'); wp_enqueue_style('default'); } add_action( 'wp_enqueue_scripts', 'my_scripts_styles' );CSS正在运行,但我没有正确加载文件脚本或代码有问题。 它包含以下测试代码:
jQuery(document).ready(function( $ ) { $('div').hide(); console.log("FFFF"); });但我得到了错误
未捕获的TypeError:undefined不是函数(匿名函数)
我想这是由于兼容模式,但到目前为止我还没有发现我做错了什么。
I am trying to make this work for quite a long time and I am still getting errors.
I am developing a new Wordpress page, version 4.0, on localhost. I am not able to get jQuery and other scripts to work.
I learned, I have to include scripts and styles in functions.php. Jquery is already registered, so I should only enqueue it. I also want one extra script.js file for other scripts. Here's my code
function my_scripts_styles() { wp_enqueue_script('jquery'); /* REGISTER ALL JS FOR SITE */ wp_register_script('my_scripts', get_template_directory_uri() . '/js/scripts.js'); /* REGISTER ALL CSS FOR SITE */ wp_register_style('default',get_template_directory_uri() . '/css/style.css'); /* CALL ALL CSS AND SCRIPTS FOR SITE */ wp_enqueue_script('my_scripts'); wp_enqueue_style('default'); } add_action( 'wp_enqueue_scripts', 'my_scripts_styles' );The CSS is working, but I the file scripts is not loaded properly or something is wrong with the code. It contains following testing code:
jQuery(document).ready(function( $ ) { $('div').hide(); console.log("FFFF"); });but I get the error
Uncaught TypeError: undefined is not a function (Anonymous function)
I guess it's due to the compatibility mode, but I haven't found so far what I am doing wrong.
最满意答案
你可以写
$(function() { $('div').hide(); console.log("FFFF"); });You won't believe that.
jQuery(document).ready(function($) { $('div').hide(); console.log("FFFF"); });IS A RIGHT ANSWER, only you have to have this script in a correct file!!!! Sorry, guys, I have copied the files from another project and I was changing a wrong file. This is very stupid, but finally it's working!
更多推荐
发布评论