【问题标题】:Wants to show the html tags as a string on the rails view with javascript想要使用 javascript 在 rails 视图上将 html 标签显示为字符串
【发布时间】:2014-12-03 07:00:29
【问题描述】:

我试图在谷歌地图标记点击事件中将 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));
            }

【问题讨论】:

  • 所以你在搜索
  • 我们尝试过,但没有成功。
  • 请显示结果和所需的 HTML 部分
  • 我想在谷歌地图标记中显示 my_string = "Test String" 这个字符串作为标题。
  • 更新了两种情况的帖子

标签: javascript jquery html ruby-on-rails ruby-on-rails-3


【解决方案1】:

我认为你在定义 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>

希望对你有帮助

【讨论】:

    【解决方案2】:

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

    所以,你的字符串会变成:

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

    【讨论】:

      猜你喜欢
      • 2021-11-04
      • 1970-01-01
      • 2016-02-12
      • 2013-11-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多