将HTML编码的字符串附加为HTML,而不是文本 [英] Appending HTML-encoded string as HTML, not as text

查看:125
本文介绍了将HTML编码的字符串附加为HTML,而不是文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图追加这个字符串:

 & lt; div& gt; hello& lt; / div& gt; 

作为HTML节点,而不是附加HTML,它只是附加文本:

 < div>你好< / div> 

如何让jQuery将这个附加为HTML节点,而不仅仅是文本? b
$ b

如果可能的话,我想要一个适用于嵌套div和文本的解决方案。

  //这是您的字符串:) 
var myString =& lt; div& gt; hello& lt; / div& gt;;

//这是htmlDecode字符串的一种方式...详情请参阅下面的链接。
myString = $(< div />)。html(myString).text();

//这是将html代码附加到您的div(您没有ID为:P)
$(#TheDivIWantToChange)。append(myString);

从输入字段读取属性时HTML编码丢失


I'm trying to append this string:

&lt;div&gt; hello &lt;/div&gt;

as an HTML node, but instead of appending HTML it just appends the text:

<div> hello </div>

How do I make jQuery append this as an HTML node, not just text?

I'd like a solution that works both for nested divs AND text, if possible.

解决方案

// This is your string :)
var myString = "&lt;div&gt; hello &lt;/div&gt;";

// This is a way to "htmlDecode" your string... see link below for details.    
myString = $("<div />").html(myString).text();

// This is appending the html code to your div (which you don't have an ID for :P)
$("#TheDivIWantToChange").append(myString);

HTML-encoding lost when attribute read from input field

这篇关于将HTML编码的字符串附加为HTML,而不是文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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