附加HTML转义文本:jQuery [英] Append HTML-escaped text: jQuery

查看:119
本文介绍了附加HTML转义文本:jQuery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我习惯于使用jQuery的 .append()方法将文本或HTML添加到预先存在的元素的末尾。我正在使用jQuery的 .text()来转义可能包含HTML的字符串。不幸的是,似乎没有一个jQuery方法将 .text()方法的结果附加到元素而不是替换其内容。

I'm used to using jQuery's .append() method to add text or HTML onto the end of a pre-existing element. I'm currently using jQuery's .text() to escape strings that could potentially contain HTML. Unfortunately, there doesn't seem to be a jQuery method that will append the results of the .text() method to an element instead of replacing its contents.

有没有办法追加,而不是替换,这个转义的文本到一个元素?还是有更好的方法来转义包含HTML的字符串?

Is there a way to append, instead of replace, this escaped text to an element? Or is there a better way to escape strings containing HTML?

谢谢。

- 编辑 -

一点点上下文:我正在动态构建一个HTML字符串,所以我需要能够以编程方式添加多个元素与转义的内容

A little more context: I'm building an HTML string dynamically, and so I'll need to be able to add multiple elements with escaped content programmatically.

推荐答案

您可以创建一个虚拟元素来保存 .text()然后可以附加到您的目标元素:

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(...);

这篇关于附加HTML转义文本:jQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆