谷歌地图JS iFrame标题?

时间:2018-02-27 15:24:07

标签: javascript html google-maps

是否可以将标题属性添加到谷歌地图iframe中,如果它是由javascript呈现的那样?

以下是在html页面中如何呈现它的示例。

<div id="googleMap" style="width:100%;height:400px;" title="test"></div>

<script>
function myMap() {
var mapProp= {
    center:new google.maps.LatLng(51.508742,-0.120850),
    zoom:5,
};
var map=new google.maps.Map(document.getElementById("googleMap"),mapProp);
}
</script>

据我所知,如果直接引用iframe,您可以添加标题,例如

<iframe title="test"/>

但不确定在以这种方式制作地图时是否可以改变它。

1 个答案:

答案 0 :(得分:4)

Google Maps API提供了一种向其地图添加事件监听器的方法。因为您只需要使用一次: https://developers.google.com/maps/documentation/javascript/reference/3/event#event.addListenerOnce

在添加以下内容后,我能够通过Lighthouse A11y审核:

google.maps.event.addListenerOnce(map, 'idle', () => {
  document.getElementsByTagName('iframe')[0].title = "Google Maps";
})

(在地图变量下方添加片段。)