将HTML编码的字符串附加为HTML,而不是文本

编程入门 行业动态 更新时间:2024-10-28 14:22:25
本文介绍了将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,而不是文本

本文发布于:2023-06-13 04:27:54,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:加为   字符串   而不是   文本   HTML

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!