【问题标题】:jQuery ID Selector Not Working in Google Maps [duplicate]jQuery ID 选择器在 Google 地图中不起作用 [重复]
【发布时间】:2013-06-10 17:22:19
【问题描述】:

尝试从纯 jQuery 方式生成 Google 地图,但当我使用 ID 选择器时无法渲染地图:

var map = new google.maps.Map($("#map_canvas"), mapOptions);

当我在简单的 JavaScript 方法中使用查找元素 id 时,地图正在工作:

var map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);

你能帮我理解为什么会这样吗?

【问题讨论】:

    标签: jquery google-maps-api-3


    【解决方案1】:

    快速搜索发现:

    它需要一个 DOM 元素,但 $('#map_cavas') 返回一个 jQuery 对象。如果您想使用 jQuery 选择器,请执行以下操作:

    var map = new google.maps.Map($("#map_canvas")[0], mapOptions);
    

    来源:Google maps (V3) - Map container selector (using jquery)

    【讨论】:

    • 嗨 tymeJV 我更新了您发布的代码,但仍然无法正常工作!我也使用了 .get(0) 方法,但没有!
    猜你喜欢
    • 1970-01-01
    • 2013-10-07
    • 2016-02-25
    • 2015-10-30
    • 2017-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多