尝试调用加载了的函数时,我遇到一个方法未定义错误wp_enqueue_script
从…起site.js
.
我猜这是因为jQuery ready包装器造成的范围?我该怎么避开呢?
site.js (已加载head
)
jQuery(document).ready(function($) {
$(window).load(function() { // wait until everything has loaded
doSomething(); // results in face palm
});
});
my-script.js (加载在页脚中)
jQuery(document).ready(function($) {
function doSomething() {
alert("Time for a coffee break!");
}
});
functions.php
wp_register_script( \'my-script\', get_template_directory_uri().\'/js/my-script.js\', array( \'jquery\' ), \'\', true );
wp_enqueue_script( \'my-script\' );
// Pass in PHP variables
wp_localize_script( \'my-script\', \'my_script\', localize_post_vars() );