谷歌地图事件对象的元素列表后缺少]

missing ] after element list for google maps event object

本文关键字:列表 元素 事件 对象 谷歌地图      更新时间:2023-09-26

我试图调用moreInfo()函数上的onclick事件,并给它的点击事件的信息。

下面是代码示例:
google.maps.event.addListener(SomeArea, 'click', showTB);
function showTB(event) {
    //content has the map click event
    var contentString = event.latLng +
        '<br>' + '<button onclick="moreInfo('+event+')">More</button>';

    infoWindow.setContent(contentString);
}

这里是相关函数moreInfo

function moreInfo(e){
    var overlayContent = e.latLng
    document.getElementById("content").innerHTML = overlayContent;
}
firebug输出:

SyntaxError: missing] after element list

moreInfo([对象对象])

我该如何解决这个问题?

infoWindow的内容也可能是DOMNode的内容。

创建DOMNode并将按钮插入该节点,然后您将能够对按钮应用单击侦听器(并传递任何类型的参数):

function showTB(event) {
  var contentElement = document.createElement('div'),
      btn            = document.createElement('button');
  contentElement.appendChild(document.createTextNode(event.latLng));
  contentElement.appendChild(document.createElement('br'));
  contentElement.appendChild(btn);
  btn.appendChild(document.createTextNode('More'));
  google.maps.event.addDomListener(btn,'click',function(){ moreInfo(event);})
  infoWindow.setContent(contentElement);
}

演示: http://jsfiddle.net/doktormolle/N5Etg/