在URL中查找并替换特殊字符 - jQuery

时间:2017-01-06 09:56:48

标签: javascript jquery iframe

我正在使用Google Maps iframe在我的网站上生成地图。 iframe看起来像这样:

<iframe id="googlemap" width="199" height="199" frameborder="0" style="border:0" src="https://www.google.com/maps/embed/v1/place?key=KEY&zoom=14&language=nb&q=Møllesvingen+2,0854+OSLO"></iframe>

如果您看到属性src,您会看到我想用%C3%B8(UTF-8字节)替换特殊字符“ø”。

我已尝试过几个替换功能,但似乎没有任何效果。 以下是我现在正在尝试的内容:

var src;

src = $('#googlemap').attr('src');

src.replace('ø', '%C3%B8');

但它不起作用。希望有人可以提供帮助。

提前致谢!

3 个答案:

答案 0 :(得分:3)

您需要添加编码。 试试这个,

src = $('#googlemap').attr('src');
src = encodeURI(src);
$('#googlemap').attr('src', src);

答案 1 :(得分:0)

str.replace(/[ø]/g,'%C3%B8');

使用正则表达式来解决它。上面的代码将替换特殊字符。

答案 2 :(得分:0)

确实如此。 replace函数返回一个新字符串。将新字符串存储到变量中并更改src。

var src;
var newStr;

src = $('#googlemap').attr('src');

newStr = src.replace('ø', '%C3%B8');

console.log(src);
console.log(newStr);

$('#googlemap').attr('src', newStr);
相关问题