[英]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.