我在脚本中添加子模板目录.php代码:
<?php
function load_styles_and_scripts() {
wp_register_script (
'myScript',
get_template_directory_uri().'/script.js',
array('jquery'),
'1.11.0',
true
);
wp_enqueue_script ('myScript');
}
add_action('wp_enqueue_scripts', 'load_styles_and_scripts');
我的脚本.js文件包含:
$( document ).ready(function() {
console.log( "ready!" );
alert("Hello! I am an alert box!");
});
文件夹/路径
我将WordPress中的可选主题设置为活动主题。CSS加载正确,但脚本中的javascript.js不是 - 在网站上没有任何差异。
我整天都在为此而战。马上感谢;)
脚本已加载 - 火虫
要将 jQuery 与 WordPress 一起使用,您必须使用字符串"jQuery"而不是"$"。例如:
jQuery('#id').show();
如果您希望覆盖 jQuery 函数并使用"$",请查看此引用 https://digwp.com/2011/09/using-instead-of-jquery-in-wordpress/。因此,对于您的示例,请执行如下操作:
jQuery(document).ready(function() {
console.log('ready!');
alert('Hello! I am an alert box!');
});