在InfoWindows中调用函数 [英] Function calls within InfoWindows
问题描述
infowindow的html很简单..信息窗口的html将包含一个
pre $ input $输入类型=按钮value =显示更多onclick =showMore('+ shopId +');
相关shopId对于每一个标记都会有明显的不同......
问题是showMore函数必须声明为JavaScript的全局函数,否则infowindow不会'找到它。
让我们说所有的代码(生成地图,放置标记,声明infowindows等)都在一个名为的函数中showShops (){}
,并且 showMore(id)
函数在showShops()函数中,我可以告诉onclick事件调用showShops函数中的showMore()函数?
为了检查我的代码,我将它更改为 onclick =alert(' + shopId +')
..我正确地获得了相关商店ID的提醒。
infowindow的内容可以是字符串,也可以是节点
。它必须使用将在 showShops()
范围内创建的节点。一个节点:$ b
$ b
var content = document.createElement('div'),
button;
content.innerHTML ='一些文字< br />';
button = content.appendChild(document.createElement('input'));
button.type ='button';
button.value ='click me!'
google.maps.event.addDomListener(button,'click',function(){
showMore(id);})
简单演示: http://jsfiddle.net/doktormolle/8ZsSp/
Often I have maps with a whole lot of markers (let's say representing shops). I have infowindows that display basic info on the shop represented by the marker, then, on the infoWindow HTML I like to put a button that, for example, says "Details". The html of the infowindow is simple.. the html of the info window would include an
input type="button" value="Show More" onclick="showMore(' + shopId + ');
The relevant shopId would be obviously different for every marker...
The problem is that the showMore function has to be declared as a global function to the javascript otherwise the infowindow doesn't find it.
Lets say that all the code (generating map, placing markers, declaring infowindows, etc) is in a function called function showShops() {}
, and the showMore(id)
function is inside the showShops() function, HOw can I tell the "onclick" event to call the showMore() function inside the showShops function?
Just to check my code, I've changed it to onclick="alert(' + shopId + ')
.. and I correctly get an alert with the relevant shop id..
The content of a infowindow may either be a string or a node.
To achieve it you must use a node that will be created inside the scope of showShops()
Sample-creation of such a node:
var content=document.createElement('div'),
button;
content.innerHTML='some text<br/>';
button=content.appendChild(document.createElement('input'));
button.type='button';
button.value='click me!'
google.maps.event.addDomListener(button,'click', function(){
showMore(id);})
Simple Demo: http://jsfiddle.net/doktormolle/8ZsSp/
这篇关于在InfoWindows中调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!