我觉得这应该是一个简单的修复方法,但我无法让它发挥作用。我正在使用谷歌地图运行jQuery商店定位器脚本。该脚本需要两个javascript文件,jquery和google maps api。最后,它调用实际函数来运行定位器。文件如下:
jlocator.min.js
jplist.min.js
jlocator.activate.js (this is where is put the document.ready script)
jquery
google api script
我让定位器在非wordpress测试站点中运行得非常好。但是在Wordpress中,商店定位器脚本根本不会运行。这是在我的functions.php页面
中function my_map_scripts() {
if ( is_page_template( 'template-project-map.php' ) ) {
wp_enqueue_script(
'jplist-custom-script',
get_stylesheet_directory_uri() . '/js/jplist.min.js',
array( 'jquery' )
);
wp_enqueue_script(
'jlocator-custom-script',
get_stylesheet_directory_uri() . '/js/jlocator.min.js',
array( 'jquery' )
);
wp_enqueue_script(
'jlocator-activate-script',
get_stylesheet_directory_uri() . '/js/jlocator.activate.js',
array( 'jquery' )
);
wp_enqueue_script(
'google-maps',
'https://maps.googleapis.com/maps/api/js'
);
}
}
add_action( 'wp_enqueue_scripts', 'my_map_scripts' );
我已经将.js文件排入队列,并且它们正在加载。我在每个文件中添加警报只是为了仔细检查。我也曾一度使用wp_script_is来确保jQuery正在运行。
问题似乎出现在我的文档准备代码中。我使用以下内容:
jQuery(document).ready(function(){
jQuery('#jlocator').jlocator();
});
这无所事事。我甚至尝试发出警报,看看是否可行:
jQuery(document).ready(function(){
jQuery('#jlocator').jlocator();
alert("activated");
});
再一次,没有。但如果我删除这一行:
jQuery('#jlocator').jlocator();
所以代码为:
jQuery(document).ready(function(){
alert("activated");
});
警报开火很好。我也试过做类似下面代码的事情只是为了看看它是否可行,但它也没有用。 (.panel是我页面上的div。)
jQuery(document).ready(function(){
jQuery('.panel').hide();
});
上面的代码在我的非Wordpress测试网站上运行良好。似乎每当我使用以下代码执行某些操作时,它会关闭其他所有内容。
jQuery('sometext')....
我在这里缺少一些简单的东西吗?
另外,我已经尝试将文档编写为:
$(document).ready(function(){
$('#jlocator').jlocator();
alert("activated");
});
和
jQuery(document).ready(function($){
$('#jlocator').jlocator();
alert("activated");
});
但仍然无效。任何建议将不胜感激。
答案 0 :(得分:0)
问题现在解决了。出于某种原因,jQuery没有加载,即使我在调用我的js文件之前在我的函数中设置了依赖项。我不确定为什么会这样。但是我将以下代码添加到我的functions.php页面,它开始工作:// Load jQuery if ( !is_admin() ) { wp_deregister_script('jquery'); wp_register_script('jquery', ("https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"), false); wp_enqueue_script('jquery'); }