[英]How to enqueue jquery script in WordPress
I'm trying to load a simple jQuery script into a WP footer (using an Avada child theme). 我正在尝试将一个简单的jQuery脚本加载到WP页脚中(使用Avada子主题)。 I've done it before, but nothing's happening.
我以前做过,但是什么也没发生。 Can anyone spot my error?
谁能发现我的错误?
In functions.php
: 在
functions.php
:
add_action( 'wp_enqueue_scripts', 'wpalert' );
function wpalert() {
wp_register_script('wpalert', '--hardcoded root--', array ( 'jquery' ), null, true);
wp_enqueue_script('wpalert');
}
In my .js
file: 在我的
.js
文件中:
$(document).ready(function(){
alert("test");
})
There are 2 things that you need to change: 您需要更改两件事:
1) wp_enqueue_script('wpalert');
By wordpress documentation, you must use the pattern: 根据wordpress文档,您必须使用以下模式:
function my_function_scripts() {
wp_enqueue_script( 'wpalert', get_template_directory_uri() . '/js/wpalert.js', array('jquery'));
}
add_action( 'wp_enqueue_scripts', 'my_function_scripts' );
2) To use jQuery in Wordpress, you need to use no conflict mode: 2)要在Wordpress中使用jQuery,您需要使用无冲突模式:
// $() will be the alias for jQuery()
jQuery( document ).ready( function( $ ) {
} );
More references: https://developer.wordpress.org/reference/functions/wp_enqueue_script/ 更多参考: https : //developer.wordpress.org/reference/functions/wp_enqueue_script/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.