Javascript Google Maps API V3窗口加载事件侦听器在某些浏览器中不工作
奇怪的行为: maps API中的on events的最后一部分说Javascript Google Maps API V3窗口加载事件侦听器在某些浏览器中不工作,javascript,google-maps,google-maps-api-3,dom-events,onload-event,Javascript,Google Maps,Google Maps Api 3,Dom Events,Onload Event,奇怪的行为: maps API中的on events的最后一部分说 > If you've been reading the documentation this far, you're probably > already familiar with one DOM event: the window.onload event, which > we've handled within the <body> tag. We use this event to t
> If you've been reading the documentation this far, you're probably
> already familiar with one DOM event: the window.onload event, which
> we've handled within the <body> tag. We use this event to trigger the
> initial Javascript code once an HTML page is fully loaded, as shown
> below: <script> function initialize() {
>
> // Map initialization
>
> } </script> <body onload="initialize()"> <div
> id="map_canvas"></div> </body> Although this event is attached to the
> <body> element here, this event is really a window event indicating
> that the DOM hierarchy below the window element has been fully built
> out and rendered. Although easy to understand, having an onload event
> within a <body> tag mixes content with behavior. Generally, it is good
> practice to separate your content code (HTML) from your behavioral
> code (Javascript) and provide your presentation code (CSS) separately
> as well. You can do so by replacing the inline onload event handler
> with a DOM listener within your Maps API Javascript code like so:
> <script> function initialize() {
>
> // Map initialization
>
> }
> google.maps.event.addDomListener(window, 'load', initialize);
> </script> <body> <div id="map_canvas"></div> </body>
>如果到目前为止您一直在阅读文档,您可能
>已经熟悉一个DOM事件:window.onload事件
>我们已经在标签内处理了。我们使用此事件触发
>HTML页面完全加载后的初始Javascript代码,如图所示
>下面:函数初始化(){
>
>//映射初始化
>
>}id=“map\u canvas”>尽管此事件已附加到
>元素,此事件实际上是一个窗口事件,指示
>窗口元素下面的DOM层次结构已经完全构建
>拿出并呈现。虽然很容易理解,但是有一个onload事件
>在标签中,内容与行为混合在一起。一般来说,它是好的
>练习将内容代码(HTML)与行为代码分开
>分别编写代码(Javascript)和提供演示代码(CSS)
>还有。可以通过替换内联onload事件处理程序来实现
>在Maps API Javascript代码中使用DOM侦听器,如下所示:
>函数初始化(){
>
>//映射初始化
>
> }
>google.maps.event.addDomListener(窗口“加载”,初始化);
>
不知何故,google.maps.event.addDomListener(窗口“加载”,初始化)
不适用于Firefox/Safari,但
适用于。。。
你知道为什么吗?你能详细谈谈“不起作用”吗。它从不运行,还是抛出错误?它不会启动(从GoogleMaps事件列表输入)您可以链接到演示该问题的JSFIDLE吗?我以前在addDomListener上没有遇到过任何问题,所以可能有第二个原因。FWIW我在Chrome上遇到了同样的问题,所以问题不限于Safari/Firefox。很久以前,我只是试过一把小提琴。它就是这样工作的