【问题标题】:How to show stars inside a Google Map callout box?如何在谷歌地图标注框中显示星星?
【发布时间】:2010-12-30 19:38:56
【问题描述】:

预期:在 Google 地图标注框中看到 1-5 颗星
实际:页面闪烁并在页面顶部仅显示一长串星星.

我怀疑我不应该使用 document.write?

<html>
    <head>

    <script type="text/javascript">

    //some Google Maps API code

         function showstar(x)
       {
        var i = 0;
        while (i < x)
        {
         document.write("<img src=\"img/star.gif\" width=\"15\" height=\"15\" />");
         i++;
        }
       }

       var html = "Review stars: " showstar(star)

       GEvent.addListener(marker, 'click', function() {
          marker.openInfoWindowHtml(html);
          });
          return marker;
        }

      </script>
    </head>

      <body onload="load()" onunload="GUnload()">
        <div id="map" style="width: 700px; height: 500px"></div>
      </body>

    </html>

【问题讨论】:

    标签: javascript google-maps


    【解决方案1】:

    正确,您不应该像这样使用 document.write - document.write Replaces All Body Content 在页面加载完成之前执行。

    而不是document.write,返回函数末尾的HTML字符串:

    function showstar(x) {
        var i = 0, starsHTML = "";
        while (i < x) {
            starsHTML += "<img src=\"img/star.gif\" width=\"15\" height=\"15\" />";
            i++;
        }
        return starsHTML;
    }
    

    PS 我假设 star 在使用之前已定义并分配了一个值,您没有在此处的代码中提供。

    【讨论】:

      猜你喜欢
      • 2012-05-11
      • 1970-01-01
      • 1970-01-01
      • 2017-05-06
      • 2010-11-29
      • 1970-01-01
      • 2023-03-04
      • 2011-08-24
      • 2020-10-24
      相关资源
      最近更新 更多