我在按钮切换后使用 jquery 加载谷歌地图:
function loadScript() {
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = 'http://maps.googleapis.com/maps/api/js?v=3.11&key=AIzaSyCK3XUMWOH0GIiuj3VeprakKZXoo_nDV08&sensor=true&' +
'callback=initialize';
document.body.appendChild(script);
}
(function ($) {
$(document).ready(function () {
//Show map on category pages
$(function () {
$("#showonmap a").toggle(function () {
$(this).children("#show_map").hide();
$(this).children("#hide_map").show();
//Reveal google maps multi
//First create empty divs, if they exist don't create them
if ($("#googlemap_multi").length == 0) {
$("#subheadergradient").before('<div class="googlemap_margin"></div><div id="googlemap_multi"></div><div class="googlemap_margin"></div>');
}
$(".googlemap_margin").fadeIn("slow", function () {
$("#googlemap_multi").animate({
height: 400
}, "slow", function () {
//load google maps
loadScript().load();
});
});
}, function () {
$(this).children("#hide_map").hide();
$(this).children("#show_map").show();
//Hide google maps multi
$("#googlemap_multi").animate({
height: 0
}, "slow", function () {
$(".googlemap_margin").fadeOut("slow");
});
});
});
});
}(jQuery));
但这会产生错误:
Uncaught TypeError: Cannot call method 'load' of undefined localhost:482
(anonymous function) localhost:482
d.complete jquery.min.js:4
f.fx.step jquery.min.js:4
h jquery.min.js:4
f.extend.tick
所以切换回功能永远不会被执行。
这部分:
//Hide google maps multi
$("#googlemap_multi").animate({height: 0},"slow", function(){
$(".googlemap_margin").fadeOut("slow");
});
问题是什么?
泰。