gpt4 book ai didi

javascript - 谷歌地图,格式化字符串。 (删除撇号)

转载 作者:太空宇宙 更新时间:2023-11-04 12:52:32 26 4
gpt4 key购买 nike

我目前正在制作谷歌地图,我使用此代码将一些内容从 CMS 加载到标记中。

function showMarker(index) {
infowindow.setContent("<div class=\"map_marker\">" + markerData[index].content + "</div>");
infowindow.open(map, markers[index]);

map.panTo(markers[index].getPosition());

showAttractionInfo(index);
}

但是,当前的问题是,如果通过 CMS 输入撇号或引号,将会破坏 map 并且不会显示。

我需要一种方法来格式化 markerData[index].content,然后将其放入 map 中,删除撇号和引号。

我尝试使用 replace 但它对我根本不起作用。有什么建议吗?

编辑:

到目前为止,我已经尝试过这些解决方案:

infowindow.setContent("<div class=\"map_marker\">" + markerData[index].content.replace(/'/g, '') + "</div>");

    var description = markerData[index].content;
description.replace(/'/g, '');
infowindow.setContent("<div class=\"map_marker\">" + description + "</div>");

 var description = markerData[index].content;
description.replace("'", "");
infowindow.setContent("<div class=\"map_marker\">" + markerData[index].content + "</div>");

var description = markerData[index].content;
description.replace("'", "&#39;");
infowindow.setContent("<div class=\"map_marker\">" + markerData[index].content + "</div>");

var description = markerData[index].content;
description.replace(/'/g, '&#39;');
infowindow.setContent("<div class=\"map_marker\">" + markerData[index].content + "</div>");

这是一个标记数据的示例(带撇号)

72 Drymen's Road, Bearden, Glasgow, G61 2RH

最佳答案

您需要将那些双引号转换为单引号。因为在你的行中

infowindow.setContent("<div class=\"map_marker\">" + markerData[index].content + "</div>");

您已经使用双引号来创建字符串。使用这样的东西:

var modified = markerData[index].content.replace(/"/g, "'");
infowindow.setContent("<div class=\"map_marker\">" + modified + "</div>");

关于javascript - 谷歌地图,格式化字符串。 (删除撇号),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26057051/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com