javascript Google 地图 (V3) - 地图容器选择器(使用 jquery)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3839074/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Google maps (V3) - Map container selector (using jquery)
提问by Pedro Gil
I'm trying to solve a tricky problem in Google Maps (api V3)
我正在尝试解决 Google Maps (api V3) 中的一个棘手问题
Works nicely:
效果很好:
var map = new google.maps.Map(document.getElementById("map_container"), myOptions);
Doesn't Work if I try to use a jQuery selector
如果我尝试使用 jQuery 选择器,则不起作用
var map = new google.maps.Map($('#map_container'), myOptions);
Thank you in advance
先感谢您
Cheers
干杯
Pedro
佩德罗
回答by reko_t
It expects a DOM element, but $('#map_container')returns a jQuery object. If you want to use a jQuery selector, do:
它需要一个 DOM 元素,但$('#map_container')返回一个 jQuery 对象。如果要使用 jQuery 选择器,请执行以下操作:
var map = new google.maps.Map($('#map_container')[0], myOptions);
Or you can also use .get(0)instead of [0], this returns the actual DOM object.
或者您也可以使用.get(0)代替[0],这将返回实际的 DOM 对象。

