单击InfoWindow内容
onclick in InfoWindow content
我在代码中遇到了一个小问题,我试图用javascript在InfoWindow的内容中启动一个函数,但我不知道为什么,我很难做到这一点。我在stackoverflow和其他地方看了很多关于它的话题,完全应用了我在这些话题上看到的内容,但。。。
注意:这是我整个代码的一部分,这就是为什么"infobulle"似乎没有被声明的原因。同样,"this.jsonActivity"answers"mapView"也会在这部分代码之前正确初始化。
这里,参数中的"newActivity"是来自谷歌地图API的Marker,我试图做的是当我们点击它时,在当前标记旁边显示一个InfoWindow。一切都很好,整个文本都正确地显示在InfoWindow中,但问题是当我点击按钮时,我无法调用"alertMessage()"方法,什么也没发生。。。我真的不知道为什么!!
好吧,这是我的代码,谢谢你的帮助:)
google.maps.event.addListener(newActivity, "click", function() {
if(infobulle)
infobulle.close();
var contenu = '<strong>' + this.jsonActivity.title + '</strong><br/>' +
this.jsonActivity.description + '<br/>' +
'<h3>Routing:</h3>' +
'<button onclick="alertMessage()">Click me</button>';
infobulle = new google.maps.InfoWindow({
content: contenu,
maxWidth: 120
});
infobulle.open(mapView, this);
function alertMessage() {
alert("alertMessage");
}
});
编辑:
这太完美了,现在开始工作了!
我已经尝试了你们所有的解决方案,只有一个对我有效,那就是宣布函数为全局的解决方案。感谢Molle博士!
现在我把我为另外两个解决方案所做的尝试放在下面:
google.maps.event.addListener(newActivity, "click", function() {
if(infobulle)
infobulle.close();
var contenu = '<strong>' + this.jsonActivity.title + '</strong><br/>' +
this.jsonActivity.description + '<br/>' +
'<h3>Routing:</h3>' +
'<button id="myAlert">Click me</button>';
infobulle = new google.maps.InfoWindow({
content: contenu,
maxWidth: 120
});
infobulle.open(mapView, this);
document.getElementById("myAlert").addEventListener(function() {
alert("something");
});
});
对于Jared Smith提出的解决方案。就像以前一样,除了点击按钮外,其他一切都显示正确,什么都没发生。
对于Alexander提出的解决方案:
google.maps.event.addListener(newActivity, "click", function() {
if(infobulle)
infobulle.close();
var contenu = '<strong>' + this.jsonActivity.title + '</strong><br/>' +
this.jsonActivity.description + '<br/>' +
'<h3>Routing:</h3>' +
'<button onclick="(function() {
alert('"something'");
})();">Click me</button>';
infobulle = new google.maps.InfoWindow({
content: contenu,
maxWidth: 120
});
infobulle.open(mapView, this);
});
这一次,即使是我应该点击按钮的元素也不会出现。。。
对于这两种解决方案,也许我没有正确使用它们。。。所以,如果你想说什么,请继续:)
编辑(2):好了,现在我有另一个问题:如果我想把一个变量作为函数的参数,我该怎么做?仅仅输入变量的名称作为参数是不起作用的。
我试过:
var contenu = '<strong>' + this.jsonActivity.title + '</strong><br/>' +
this.jsonActivity.description + '<br/>' +
'<h3>Routing:</h3>' +
'<button onclick="alertMessage(''something'')">Click me</button>';
window.alertMessage = function(thing) {
alert(thing);
};
这是有效的,因为我直接把字符串作为参数。
但如果我申报:
var text = "something";
var contenu = '<strong>' + this.jsonActivity.title + '</strong><br/>' +
this.jsonActivity.description + '<br/>' +
'<h3>Routing:</h3>' +
'<button onclick="alertMessage(text)">Click me</button>';
window.alertMessage = function(thing) {
alert(thing);
};
它已经不起作用了,你知道怎么解决吗?
使函数全局可访问(目前不可访问):
window.alertMessage=function() {
alert("alertMessage");
}
而不是
"<button onclick='blah'>"
你需要
"<button id='myAlertButton'>"
然后在infoWindow添加到DOM 之后的eventListener回调中
document.getElementById('myAlertButton').addEventListener(function(){
alert('Whatever');
});
InfoWindow
接受一个HTMLElement
节点作为其content
选项。因此,您可以通过JS构建HTML,并添加事件JS land,例如:
// Create Nodes
const ul = document.createElement('ul');
const setOrigin = document.createElement('li');
// Set hierarchy
ul.appendChild(setOrigin);
setOrigin.appendChild(document.createTextNode('Set Origin'));
// Add event listener
setOrigin.addEventListener('click', () => console.log('Set origin and so on…'));
// Set root node as content
const contextMenu = new google.maps.InfoWindow({
content: ul,
});
- CSS-如何定位内容数据标题
- YUI3 IO实用程序是否可以根据给定的内容类型标头值自动序列化数据
- 如何用更合适的内容替换document.write
- 将事件聚焦/模糊在可编辑内容的元素上
- JSON重构(合并内容)与javascript
- RegExp:匹配Javascript中除regex值之外的所有内容
- 测试数组中每个项的内容
- 使用jQuery从原始页面内容创建iframe
- 在DOM中查找一个模式并替换它's的内容使用jquery
- 引导程序:在导航栏中,显示悬停在单个位置的基于Li Link的不同内容
- 检测页面上某个元素中选择(突出显示)或单击的内容
- 按我自己的类克隆另一个元素的内容和顺序
- document.styleSheets不返回任何内容
- 当单击第一个李时,它显示内容一,当单击第二个李时,它显示内容二
- 边栏按钮和infoWindow是否可能有不同的内容
- 强制Google InfoWindow重新绘制自身或刷新其内容
- 单击InfoWindow内容
- Google Maps infoWindow内容变量失败
- gmap API v3 ->infoWindow内容不显示
- 标记内容(infoWindow)谷歌地图