2017-02-08 4 views
0

Ich habe einen mehr auf einer Google Maps Karte platziert Marker und jeder Marker hat ein Infofenster mit folgendem Inhalt:Kann ich getElementById() in einem Maps-Infofenster in JavaScript verwenden?

  // Set the content of the infowindow of the marker. 
      var infoContent = 
       '<div id="iw-container" class="gm-style-iw">'+ 
       '<div class="iw-title">'+ 
        '<p>{{place.name}}</p>'+ 
       '</div>'+ 
       // For each photo associated with this place, add it. 
       '<div class="container-fluid">'+ 
        {% for photo in place.photo_set.all %} 
        '<div class="row">'+ 
         '<div class="col-md-4">'+ 
         '<hr>'+ 
         '<div class="">'+ 
         // Check whether the attributes have a value and display them. 
          {% if photo.info != "" and photo.info != None %} 
          '<p class="iw-info">{{ photo.info }}</p>'+ 
          {% endif %} 
          {% if photo.year != "" and photo.year != None %} 
          '<p class="iw-year">{{ photo.year }}</p>'+ 
          {% endif %} 
          {% if photo.source != "" and photo.source != None %} 
          '<p class="iw-source">Bron: {{ photo.source }}</p>'+ 
          {% endif %} 
         '</div>'+ 
         // Display the image with a link to the modal view. 
         '<div class="iw-image">'+ 
          '<img id="{{ photo.id }}" class="iw-image" src="../static/images/{{ photo.image }}">'+ 
         '</div>'+ 
         '</div>'+ 
        '</div>'+ 
        {% endfor %} 
       '</div>'+ 
       '</div>'; 

Ich möchte in jedem Infofenster eine modale Ansicht für jedes Bild hinzufügen, aber ich kann das nicht tun dies:

var img = document.getElementById("{{ photo.id }}"); 

Dies ist, weil jedes Mal, wenn die Variable img Null ist.

Kann man mit getElementById() auf infowindow-Inhalte zugreifen?

+0

Könnte das sein, weil der HTML-Code für das Infowindow nicht bereits hinzugefügt wird, wenn getElementById() aufgerufen wird? – Vincent

Antwort

0

Wo rufst du an? document.getElementById("{{ photo.id }}")?

Ich konnte etwas Ähnliches tun, indem ich ein div mit einer ID im InfoWidow zurückgab und document.getElementById() in onMarkerClick anrief.

+0

Bitte geben Sie nur Antworten auf das Problem als neue Antwort, Sie können den ersten Beitrag kommentieren, wenn Sie Fragen stellen. –

Verwandte Themen