想要使用 javascript 在轨道视图上将 html 标记显示为字符串

Wants to show the html tags as a string on the rails view with javascript

本文关键字:html 显示 字符串 视图 javascript 轨道      更新时间:2023-09-26

我正在尝试在谷歌地图标记点击事件中将 html 标签显示为字符串,但它将输出显示为 html 标签转换为用户控件。

Ex- my_string ="<输入类型>测试字符串"

然后

它将显示文本字段,然后显示测试字符串。

my_string =  "< input type=text >Test String"
for (i = 0; i < locations.length; i++) {
                marker = new google.maps.Marker({
                  position: new google.maps.LatLng(locations[i][1], locations[i][2]),
                  map: map
                });
              google.maps.event.addListener(marker, 'click', (function(marker, i) {
                  return function() {
                    var s = unescape(escape(my_string));
                          infowindow.setContent("<a href='/"+locations[i][4]+"/result_list' target='_blank'>"+locations[i][0]+"</a>"+"<br>"
                                          +"<b>Title : </b>"+s+"<br>"
                                          +"<b>Currency : </b>"+locations[i][6]+"<br>"
                                          +"<b>Trade Amount : </b>"+locations[i][7]+"<br>"
                                          +"<b>List Created by : </b>"+locations[i][8]+""  );
                        infowindow.open(map, marker);
                    }

                })(marker, i));
            }

我认为您在定义变量时有错误my_string。它应该是

my_string = "<input type='text'>Test String";

"<"和"输入"之间不能没有空格。

这是我尝试过的工作代码

<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
    <title>Simple markers</title>
    <style>
      html, body, #map-canvas {
        height: 100%;
        margin: 0px;
        padding: 0px
      }
    </style>
    <script src="https://maps.googleapis.com/maps/api/js?v=3.exp"></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 marker = new google.maps.Marker({
              position: myLatlng,
              map: map,
              title: 'Hello World!'
          });
          var contentString =  "<input type='text'>Test String";
            var infowindow = new google.maps.InfoWindow({
              content: contentString
          });
          google.maps.event.addListener(marker, 'click', function() {
            infowindow.open(map,marker);
          });
        }
        google.maps.event.addDomListener(window, 'load', initialize);
    </script>
  </head>
  <body>
    <div id="map-canvas"></div>
  </body>
</html>

希望对你有帮助

如果要打印标签,则需要分别将<>替换为等效的HTML实体,&lt;&gt;

因此,您的字符串将变为:

my_string =  "&lt;input type=text&gt;Test String"