[英]How to add html element to the current textarea value, and output it as raw html
我需要為textarea中添加的用戶設計總成本。
我有這段代碼:
$("#student_teacher_profile_for_teaching_amount").keyup(function(e) {
var new_str, price, regex, str;
regex = /[0-9]+\.[0-9]{1,2}|[0-9]/;
str = $(this).val();
console.log(str);
price = str.match(regex);
if(price) {
if( $("#to_teach_ammount").length > 0 ) {
$("#to_teach_ammount").html(price[0]);
} else {
new_str = str.replace(regex, "<span id='to_teach_ammount'>" + price[0] + "</span>");
$(this).val(new_str);
}
$("#to_teach_total").val(price[0]); #this is and hiddent input filed
}
});
結果,我得到:
some text before numbers <span id='to_teach_ammount'>2</span>
在我的文本區域中。
如何將其轉換為原始HTML?
這對於普通的<textarea>
元素是不可能的,該元素僅接受純文本。 您將必須使用(富文本)插件,例如jQuery。 在這里看看: http : //www.strangeplanet.fr/work/jquery-highlighttextarea/
to_teach_total不應是文本區域。 相反,它應該是期望html的元素
然后使用$(this).html(new_str)設置html
此html()方法還可以傳遞一個函數,該函數可以將舊html作為參數並返回要設置為新html的新字符串。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.