1

我有一個簡單的谷歌地圖標記和信息窗口(如例如在谷歌地圖API文檔:在谷歌地圖InfoWidow重寫內容

<html> 
<meta charset="utf-8" /> 
<style> 
.redtext { 
    color: red; 
} 
</style> 
    <script src="http://maps.google.com/maps/api/js?sensor=false" 
      type="text/javascript"></script> 
<script type="text/javascript" src="https://code.jquery.com/jquery-1.10.2.min.js"></script> 
<script> 
function initialize() { 
    var myLatlng = new google.maps.LatLng(-25.363882,131.044922); 
    var mapOptions = { 
    zoom: 4, 
    center: myLatlng 
    }; 

    var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions); 

    var contentString = '<div id="content"><a href="#" class="iwlink" onClick="aclick()">click me</a><P class="iwtext">Hello world!'; 

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

    var marker = new google.maps.Marker({ 
     position: myLatlng, 
     map: map, 
     title: 'Uluru (Ayers Rock)' 
    }); 
    google.maps.event.addListener(marker, 'click', function() { 
    infowindow.open(map,marker); 
    }); 
} 

google.maps.event.addDomListener(window, 'load', initialize); 

function aclick(){ 
    $(".iwtext").toggleClass("redtext"); 
} 
</script> 


    <body onload="initialize()"> 
    <div id="map-canvas" style="width: 100%; height: 100%"></div> 
    </body> 
</html> 

如果你試試這個,你會看到一個信息窗口,點擊後如果你點擊一個鏈接,文字會變成紅色

但是,如果你關閉infowindow,點擊一個[X],然後再打開它,你會看到,文本再次變黑,同樣的問題,如果你再次點擊標記

是否有任何方法可以在infowindow中重寫內容來解決此問題?

+1

我解決它喜歡這樣: '函數A單擊(){ \t $ toggleClass( 「redtext 」);(「 iwtext」)。 \t contentString ='

' + $("#content").html() + "
「; \t infowindow.setContent(contentString); }' 但我不是舒爾,它是解決這一問題的好辦法也許有人知道如何更好地解決這個問題 – Evgeny

+0

我忘了?說,我做contentString和infowindow變量全球 - 但他們在所有功能之前 - '