我正在尝试使用带有以下代码的 jquery 将 html 代码存储在 textarea 字段中:
var text ="<p>Hello, my name is Sam</p>";
var textarea = d.createElement( 'textarea' ); // Creates textarea to store text
div.appendChild( textarea );
textarea.innerHTML = text;
// $("textarea").val(modified_clipboard_text); // same issue
console.log( textarea.innerHTML ); // output should not be escaped html
问题是,html 字符串被转换为转义实体(至少在 Chrome 中)。例如:<p>
转换为 <p>
最佳答案
JQuery 解决方案
你正在使用 jquery 那么为什么不使用魔法呢:
var text ="<p>Hello, my name is Sam</p>";
$('#container').append('<textarea></textarea>');
$('#container textarea').val(text);
$('#select-btn').click(function() {
$('#container textarea').select();
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id='container'></div>
</br>
<button id='select-btn'>Select</button>
关于javascript - jquery:在 Chrome 中用未转义的 html 填充文本区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33701196/