下面是我的代码片段
{% autoescape on %}
<li><h4 id="instruction" style = "word-wrap: break-word">{{question_1}}</h4></li>
{% endautoescape %}
说{{question_1}}(服务器端的变量渲染)是“this is an example ”,输出就像
this is *an example*
但是当我将内容从 javascript 更改为类似内容时:
document.getElementById("instruction").innerHTML = "this is <i>example 2</i>";
html 的内容发生变化,但不是斜体。我该怎么做。谢谢
最佳答案
刚刚发现问题出在一个没有很好解码的标签上。所以,我只是这样做了
document.getElementById("instruction").innerHTML = "this is <i>example 2</i>".replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"').replace(/'/g, '\'');
这对我来说效果很好。谢谢。
关于javascript - 在 javascript 中格式化 Html 字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20843378/