无法让我的js脚本在wordpress中工作(Can't get my js script to work inside wordpress)

编程入门 行业动态 更新时间:2024-10-23 19:27:00
无法让我的js脚本在wordpress中工作(Can't get my js script to work inside wordpress)

我通过function.php在wordpress中排队了一个文件“my-script”,以便为网站添加一些jQuery功能。 主要目的是隐藏轮播控件,如果只有一个项目(一个图像或div)不幸的是我甚至不能得到wordpress甚至读取文件。 在文档加载后没有任何反应我放置了一个简单的警报。 这就是我排队的几个脚本,包括“我的脚本”

/* Enqueuing Scripts */ function enqueue_my_scripts() { wp_enqueue_script( 'jquery', 'https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js', array('jquery'), '1.12.4', true); wp_enqueue_script( 'bootstrap-js', 'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js', array('jquery'), true); // all the bootstrap JavaScript goodness wp_enqueue_script( 'my-script', get_stylesheet_directory_uri() . '/js/my-script.js' , array('jquery'), true); } add_action('wp_enqueue_scripts', 'enqueue_my_scripts'); function enqueue_my_styles() { wp_enqueue_style( 'bootstrap', 'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css' ); wp_enqueue_style( 'my-style', get_template_directory_uri() . '/style.css'); wp_enqueue_style( 'my-ot-style', get_template_directory_uri() . '/dynamic.css'); } add_action('wp_enqueue_scripts', 'enqueue_my_styles');

这是'my-script'文件的内容

$(document).ready(function(){ alert( "Test My-Script" ); });

什么都没发生。 我目前有通过CSS隐藏轮播控件的div,但是想让它工作,所以网站在隐藏之前评估它是否只有一个项目

提前致谢!!

I enqueued a file "my-script" inside wordpress through function.php in order to add some jQuery functionality to the site. The main purpose was to hide the carousel controls if only one item was present (one image or div) Unfortunately I can't even get wordpress to even read the file. I placed a simple alert to show after document is loaded and nothing happens. This is how I enqueued several scripts including "my-script"

/* Enqueuing Scripts */ function enqueue_my_scripts() { wp_enqueue_script( 'jquery', 'https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js', array('jquery'), '1.12.4', true); wp_enqueue_script( 'bootstrap-js', 'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js', array('jquery'), true); // all the bootstrap JavaScript goodness wp_enqueue_script( 'my-script', get_stylesheet_directory_uri() . '/js/my-script.js' , array('jquery'), true); } add_action('wp_enqueue_scripts', 'enqueue_my_scripts'); function enqueue_my_styles() { wp_enqueue_style( 'bootstrap', 'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css' ); wp_enqueue_style( 'my-style', get_template_directory_uri() . '/style.css'); wp_enqueue_style( 'my-ot-style', get_template_directory_uri() . '/dynamic.css'); } add_action('wp_enqueue_scripts', 'enqueue_my_styles');

and this is the contents of 'my-script' file

$(document).ready(function(){ alert( "Test My-Script" ); });

Nothing happens. I currently have the divs with the carousel controls hidden through CSS, but would like to get it working so the site evaluates if it has just one item before hiding them

Thanks in advance!!

最满意答案

在WordPress中,你不能像这样使用$来引用你自己加载的jQuery。 这是因为WordPress调用了jQuery.noConflict() 。

一种解决方案是引用jQuery而不是$ 。 如果你仍然希望你的脚本使用$ ,那么写下:

jQuery(document).ready(function($){ // ... your code that can reference $ comes here. });

不要忘记指定$ as callback函数参数。

In WordPress you cannot use $ just like that to reference your own loaded jQuery. This is because WordPress has called jQuery.noConflict().

One solution is to reference jQuery instead of $. If you still want your script to use $, then write this:

jQuery(document).ready(function($){ // ... your code that can reference $ comes here. });

Don't forget to specify the $ as callback function parameter.

更多推荐

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

发布评论

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

>www.elefans.com

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