在JavaScript中格式化HTML字符串 [英] Format Html String in javascript
本文介绍了在JavaScript中格式化HTML字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
以下是我的代码片段
Below is a fragment of my code
{% autoescape on %}
<li><h4 id="instruction" style = "word-wrap: break-word">{{question_1}}</h4></li>
{% endautoescape %}
说{{question_1}}(从服务器端渲染变量)是这是一个例子,输出将会像
say {{question_1}} (variable render from server side) is "this is an example ", the output will be like
this is *an example*
但是,当我将内容从javascript更改为如下内容:
But when i change the content from javascript to something like:
document.getElementById("instruction").innerHTML = "this is <i>example 2</i>";
html内容变化但不是italise。我是否这样做。谢谢
the content of the html changes but it is not italise. hw do i do this. Thanks
推荐答案
刚刚发现问题来自未完全解码的标签。所以,我只是这样做了
Just found out that the problem is from a tag that was not well decoded. so, i just did this
document.getElementById("instruction").innerHTML = "this is <i>example 2</i>".replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"').replace(/'/g, '\'');
这对我很好,谢谢。
这篇关于在JavaScript中格式化HTML字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文