Wordpress:脚本无法正常工作(Wordpress: Scripts are not working properly)

编程入门 行业动态 更新时间:2024-10-25 16:22:00
Wordpress:脚本无法正常工作(Wordpress: Scripts are not working properly)

我试图让这个工作很长一段时间,我仍然会遇到错误。

我正在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!

更多推荐

本文发布于:2023-08-04 23:51:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1423292.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:无法正常   脚本   工作   Wordpress   working

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!