javascript 双引号被替换为&quote;

6yt4nkrj  于 2023-05-16  发布在  Java
关注(0)|答案(4)|浏览(124)

我的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]]}
czq61nw1

czq61nw11#

将字符串输出为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>
cgyqldqp

cgyqldqp2#

var text1 = '{&quot;start&quot;:{&quot;lat&quot;:19.0759842,&quot;lng&quot;:72.87765630000001},&quot;end&quot;:{&quot;lat&quot;:18.5206624,&quot;lng&quot;:73.8567415},&quot;waypoints&quot;:[[18.8753235,73.52948409999999]]}';
var text2 = text1.replace(/&quot;/g, '\"');

alert('Your data\n' + text1);

alert('Required data\n' + text2);
6mzjoqzu

6mzjoqzu3#

如果你正在尝试在laravel中view blade中显示json数据,那么你可以尝试这样做<php echo $data?> or @php echo $data;@endphp而不是{{ $data }}

cunj1qz1

cunj1qz14#

使用decodeURI javascript函数来显示示例

var s= decodeURI('your string');

相关问题