ds1379551 2016-11-30 04:31
浏览 725
已采纳

Leaflet标记弹出文本中的换行符

I have a form to associate some text to a marker on a Leaflet map:

<form action="map.php" method="POST">
 <section>
  <label>write some text here</label>
   <textarea id="text" rows="3" name="area"></textarea>
 </section>

 <input type="submit" value="show map" />
</form>

As you can see above, the content of the textarea is passed to the page map.php, where a map is shown. On the map, the place marker is shown, and a popup contains the text posted through the textarea (variable $text):

<?php
$text = htmlspecialchars($_POST["text"]);
?>

<center>
 <div id="map" class="embed-container"></div>
</center>

<script>
  var map = L.map('map').setView([46.13, 11.11], 9);
  L.tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',
    {
      attribution: '&copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>',
      maxZoom: 17,
      minZoom: 9
    }).addTo(map);
</script>
<script>
  var icon = L.icon({
        iconUrl: 'img/gps.png',
        iconSize: [25, 25],
        });
  var marker = L.marker(['<?=$latitude;?>', '<?=$longitude;?>'], {icon: icon}).addTo(map);
  marker.bindPopup('<?=$text;?>');
</script>

The problem is that if I press ENTER while I write something in the textarea, nothing is passed to the popup marker. I tried with and such, but nothing works. They are shown in the popup text as strings, not as newlines.

Any ideas?

展开全部

  • 写回答

2条回答 默认 最新

  • duan0530 2016-11-30 05:10
    关注

    The problem is that if I press ENTER while I write something in the textarea, nothing is passed to the popup marker.

    This is inaccurate. The HTML inside the popup is receiving a newline character.

    The problem is that the newline character is being displayed as collapsed whitespace. This is the default in HTML. Let me quote the documentation for the white-space CSS property, inherited by default by all HTML elements:

    normal

    • Sequences of whitespace are collapsed. Newline characters in the source are handled as other whitespace. Breaks lines as necessary to fill line boxes.

    HTML collapses whitespace characters, including tabs and line breaks (but excluding &nbsp;s).

    HTML and whitespace

    Text inside Leaflet popups simply follow the same rule. Replace your newlines with HTML <br> line breaks, or otherwise wrap every line in a block-level element like a <div>.

    Popups and newlines

    If you are using php, the easiest way is to use the nl2br function. This is not necessarily the best way, as it is very prone to allowing XSS attacks in the hands of inexperienced developers. Using htmlspecialchars will not help if you are outputting JavaScript strings, enclosed in quotes. For these cases I recommend using json_encode to provide the quotes and the quote scaping, i.e. var foo = <?= json_encode(nl2br(str)); ?>;

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部