[英]How can i append pure string into div?
所以这是要求。 用户可以在text_field输入中插入标签。
我从这个数据库得到了数据。
var input = "test<br/><iframe src='http://stackoverflow.com/'>" ;
我只想将它append
到PURE字符串。 不是标签
但如果我追加它就像一个标签。
我想要的结果就是打印
test<br/><iframe src='http://stackoverflow.com/'>
作为纯粹的字符串。
有什么好办法吗?
HTML,
<div id="i_want_only_string">
</div>
JavaScript的,
$(function(){
var input = "test<br/><iframe src='http://stackoverflow.com/'>" ;
$("#i_want_only_string").append(input);
});
尝试附加TextNode - 使用document.createTextNode()创建它
$(function () {
var input = "test<br/><iframe src='http://stackoverflow.com/'>";
$("#i_want_only_string").append(document.createTextNode(input));
});
演示: 小提琴
正如@BillCriswell所述,如果容器#i_want_only_string
为空,则可以使用.text()之类的
$(function () {
var input = "test<br/><iframe src='http://stackoverflow.com/'>";
$("#i_want_only_string").text(input);
});
演示: 小提琴
您可以使用输入文本作为innerText
的新标记的contents
append
容器
$(function () {
var input = "test<br/><iframe src='http://stackoverflow.com/'>";
$("#i_want_only_string").append($('<span>').text(input).contents());
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.