$('.click').click(function(){
var t = 'hello /n world';
$('.target').text(t);
});
我有一个按钮,它会在点击后替换字符串。
如果我使用 text()
它会将所有字符串转换为 htmlentites
并且我无法保留换行符
如果我使用 html()
我会受到 xss 攻击)
(var t应该是从数据库中取出的文本数据)
有什么建议吗?
最佳答案
尝试使用:
var t = 'hello /n world';
t = t.replace(/\/n/g, "\n")
$('.word')[0].innerText = t;
因为\n 应该用于换行符。
关于javascript - htmlentities 中的 jquery 输出文本,换行符除外,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21641882/