如何在html页面中从不同的div调用javascript函数
我有这段代码,在同一个html页面中有两个脚本。第一个是进入一个包含谷歌地图的div,一旦用户单击地图上的一个标记,标记上的人的名字就会出现在另一个div中如何在html页面中从不同的div调用javascript函数,javascript,html,google-maps,Javascript,Html,Google Maps,我有这段代码,在同一个html页面中有两个脚本。第一个是进入一个包含谷歌地图的div,一旦用户单击地图上的一个标记,标记上的人的名字就会出现在另一个div中 <div id = "first"> <div id ="map"> <script type="text/javascript> [.. google map api code] var marker = new google.maps.Marker({
<div id = "first">
<div id ="map">
<script type="text/javascript>
[.. google map api code]
var marker = new google.maps.Marker({
position: pos3,
map: map,
icon: "../images/RFlogo.png",
// name = "roger",
// lastname = "federer",
});
marker.addListener('click', function () {
//
});
</script>
</div>
</div>
<div id ="second">
<script type="text/javascript">
write.document("Name: " + name + "\nLast name: " + last name);
//how can i do to print the name on the marker ?
</script>
</div>
使用标记的customInfo
属性完成
var marker = new google.maps.Marker({
position: uluru,
map: map,
customerInfo:{name:"Vignesh"}
});
google.maps.event.addListener(marker,"click",function(){
console.log(this.customInfo.name);
});
您不需要将脚本直接放在希望代码显示的元素中。。。您应该使用document.findElementById(“second”).innerHTML='您想要的文本';在监听器内部,单击监听器,您可以在这里使用/
,从标记器中获取所需的任何信息,并对其执行任何操作。但是您不会使用write.document()
——没有这样的东西。您也不会使用document.write()
。它确实存在,但不适用于此。但是有几种方法可以做到这一点,包括@CalvinNunes的好建议——除了getElementById
;-)