Append HTML-escaped text: jQuery

瘦欲@ 提交于 2019-11-29 07:15:54

You could create a dummy element to hold the result of .text() which can then be appended to your destination element:

$('<div/>').text('your <span>html</span> string').appendTo(...);

As I have tried many ways, I think the following method is the cleanest way to add text to whatever node you want. no stock tag needed, only plain text, which will help to avoid potential problems

$(document.createTextNode("SomePlainText")).appendTo(p);

You could just use

$(whatever).text($(whatever).text() + whatever_you_want_to_append);

EDIT for the fiddle in my comment, try this:

for ( /* some looping parameters */ ) {
    $('<li></li>') // create an li
        .text(stringWithHtml) // pass it the text, as text not html
        .appendTo('#thisIsWhatINeed'); // append it where you want it
}

jsFiddle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!