双重报价被“e;

时间:2015-09-23 12:15:57

标签: javascript jquery html

我的JavaScript变量包含一个字符串:

{"start":{"lat":19.0759842,"lng":72.87765630000001},"end":{"lat":18.5206624,"lng":73.8567415},"waypoints":[[18.8753235,73.52948409999999]]}

但是当我将它展示到HTML组件中时,它看起来像:

{"start":{"lat":19.0759842,"lng":72.87765630000001},"end":{"lat":18.5206624,"lng":73.8567415},"waypoints":[[18.8753235,73.52948409999999]]}

3 个答案:

答案 0 :(得分:4)

将字符串输出为HTML而不是纯文本,以便正确呈现HTML实体"

var s = "{"start":{"lat":19.0759842,"lng":72.87765630000001},"end":{"lat":18.5206624,"lng":73.8567415},"waypoints":[[18.8753235,73.52948409999999]]}";

//Incorrect with jQuery
$("#incorrect-jquery").text(s);

//Correct with jQuery
$("#correct-jquery").html(s);

//Incorrect with plain JavaScript
document.getElementById("incorrect-js").textContent = s;

//Correct with plain JavaScript
document.getElementById("correct-js").innerHTML = s;
div {
  margin-bottom: 20px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<strong>Incorrect with jQuery</strong>
<div id="incorrect-jquery"></div>

<strong>Correct with jQuery</strong>
<div id="correct-jquery"></div>

<strong>Incorrect with plain JavaScript</strong>
<div id="incorrect-js"></div>

<strong>Correct with plain JavaScript</strong>
<div id="correct-js"></div>

答案 1 :(得分:1)

1.01

答案 2 :(得分:-1)

使用decodeURI javascript函数来显示 例子

var s= decodeURI('your string');