展开全部
Html中特殊字符不被转义,可以使用预格式化标签。
pre 是 Preformatted text(预格式化文本) 的缩32313133353236313431303231363533e59b9ee7ad9431333339653761写。使用此标签可以把代码中的空格和换行直接显示到页面上。
例如HTML代码:
if (xx > 5) {
print "比5大!\n";
}
浏览器显示效果:
if (xx > 5) {
print "比5大!\n";
}
之间包含有类似的这种转义字符的时候总会被解析,倒是可以把所有的"&"通过程序替换成"&",但是有些本来就是"&"的也会被转换,这就错了。如何让之间包含的文本原封不动的显示出来呢?
总结如下:
解决方法有两种:
第1种:
document.getElementById('t').innerText='a';
第2种:
/*将字串转为html格式*/public String strToHtml(String s)
{
if (s==null||s.equals("")) return "";
s = s.replaceAll("&", "&");
s = s.replaceAll("
s = s.replaceAll(">", ">");
s = s.replaceAll(" ", " ");
// s = s.replaceAll("/n", "
");
// s = s.replaceAll("'", "'");
return s;
}
/*将html格式转为字串*/
public String strToHtml(String s)
{
if (s==null||s.equals("")) return "";
s = s.replaceAll("&","&");
s = s.replaceAll("
s = s.replaceAll(">",">");
s = s.replaceAll(" "," ");
//s = s.replaceAll("
","/n");
//s = s.replaceAll("'","'");
return s;
}
最后一点:jQuery的.html()方法默认会转义的,这种情况使用.text()就不会转义了。