如何传递map-canvas作为字符串,而不是从document.getElementById获取它
how to pass map-canvas as string instead of getting it from document.getElementById
是否有像这样传递字符串变量的方法:
var mapCanvas = '<div id="map-canvas"></div>';
到
var map = new google.maps.Map(mapCanvas,mapOptions);
在我的情况下,这是给我的错误,因为我动态地生成我的页面,有了这个,我的意思是我只有一个div在我的身体,生成地图和一切,然后我把它们附加到我的div.但是当我这样做,我在谷歌地图本身有错误。诸如位置未定义之类的错误。为什么会发生这种情况?
google.maps.Map
需要一个DOM对象,所以你可以先在主体中添加#map-canvas,然后在你的google地图初始化代码中使用它。
$("body").append('<div id="map-canvas"></div>');
map = new google.maps.Map(document.getElementById('map-canvas'),
mapOptions);
在这个例子中,我使用JQuery库将DIV附加到Body: $("body").append()
相关文章:
- 正在寻找比$(document).ready慢的$(window).load的替代方案
- document.open/document.write没有正确地清除chrome中的文档——这是chrome的错误吗
- 如何用更合适的内容替换document.write
- document.styleSheets不返回任何内容
- jQuery document.ready not working
- $(document).height()在刷新时随机化值(Safari 5.1.10)
- reactjs this.refs vs document.getElementById
- 如何通过adf中的document.getElementById获取inputText字段值
- window.opener.document在ie中不起作用
- contentWindow.document.body is null
- document.applet.method在Mounatin Lion上抛出safari 6+JDK7异常
- document.getElementById(“st”).click();不起作用
- $(document).height和$(window).heaght都返回相同的值-使用的是正确的doctype
- jQuery document.ready停止代码
- document.getElementById并使用id名称
- 使用jQuery 1.8.1准备好多个$(document).是否有任何开销
- document.getElementById在js中不起作用
- document.getElementById.style.backgroundImage not working
- 停止对document.ready函数的重定向/刷新
- 是否可以 document.getElementsByTagName('head')[0] ever 返回 null