简体   繁体   English

在InfoWindow中调用JavaScript函数(Google Maps)

[英]Calling a JavaScript function within an InfoWindow (Google Maps)

I'm having an issue calling a javascript function within a Google Maps InfoWindow. 我在Google Maps InfoWindow中调用javascript函数时遇到问题。 The relevant code is below: 相关代码如下:

<script type="text/javascript">
<!--
/* … unnecessary code */

function closeTo(url_slug) {
    parent.url_slug = url_slug;
    parent.$.fancybox.close();
}

var contentString = '<a href="javascript:closeTo(\'foo\');">close</a>';

var callout = new google.maps.InfoWindow({
    content:contentString
});

//-->
</script>

The above example results in a blank map. 以上示例导致空白地图。 However, if I change the content string to this: 但是,如果我将内容字符串更改为:

var contentString = '<a href="#">close</a>';

It works just fine. 它工作得很好。 Also, I've called the function from a link on the page (not an InfoWindow) and it works as well. 此外,我从页面上的链接(而不是InfoWindow)调用了该函数,它也可以正常工作。 The general idea is to respond to a click in an InfoWindow, close the fancybox, and redirect to the location provided. 一般的想法是响应InfoWindow中的单击,关闭fancybox,并重定向到提供的位置。 So, my question is this. 所以,我的问题是这个。 How do I call a function within an InfoWindow? 如何调用InfoWindow中的函数?

There is nothing inherently wrong with calling a javascript function from an infoWindow. 从infoWindow调用javascript函数没有任何内在错误 I suggest you check the JavaScript error log and try and track down what is happening once closeTo is called. 我建议您检查JavaScript错误日志,并尝试在调用closeTo时跟踪发生的情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM