这里我有一个文本框,用户可以在其中输入html标签,例如<h1>hello</h1>
然后我将该文本附加到 td 中
var text = $('textbox').val();
$('table').append('<tr><td style="padding:0px 12px;color:black">'+(text)+'</td></tr>');
现在我想要的是 td 中的文本应该是输入的文本 <h1>hello</h1>
而不是 h1 标签你好
我尝试了 escape 和 unscape 但没有帮助
最佳答案
使用这里的编码函数 HTML-encoding lost when attribute read from input field
function htmlEncode(value){
return $('<div/>').text(value).html();
}
var text = htmlEncode($('textbox').val());
$('table').append('<tr><td style="padding:0px 12px;color:black">'+(text)+'</td></tr>');
关于javascript - JavaScript 中的 html_entities,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5854435/