jQuery .append() 不呈现 html 实体

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15843109/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 15:53:12  来源:igfitidea点击:

jQuery .append() not rendering html entity

javascriptjqueryhtmlhtml-entities

提问by ThomasReggi

I have a variable with html in it like this:

我有一个带有 html 的变量,如下所示:

var html = '<div>Hello, you&#39;re awesome!</div>';

I want to append it to an element $("body").append(html)for some reason it's not decoding the HTML Entity. I tried $("body").append($(html));and that didn't work. My text is stuck inside the element and I can't individually put it together.

$("body").append(html)由于某种原因,我想将它附加到一个元素,它没有解码 HTML 实体。我试过了$("body").append($(html));,没有用。我的文本被困在元素内,我无法将它们单独放在一起。

Is there any way to append html with a text-based entity inside it into another element, and have the html entity render on the page?

有什么方法可以将带有基于文本的实体的 html 附加到另一个元素中,并使 html 实体呈现在页面上?

I've read a bunch of posts on stackoverflow reguarding html entities and it seems that none of them include the html & text within a variable like this.

我已经阅读了一堆关于 stackoverflow 保护 html 实体的帖子,似乎它们都没有在这样的变量中包含 html 和文本。

回答by pala?н

Try this:

尝试这个:

var html = $('<div>Hello, you&#39;re awesome!</div>');
$("body").append(html)

Demo here

演示在这里

回答by Edward

It could be possible that your page did not finish loading while the jQuery code was trying to append the content. Try:

当 jQuery 代码尝试附加内容时,您的页面可能没有完成加载。尝试:

$(document).ready(function (){
  var html = '<div>Hello, you&#39;re awesome!</div>';
  $('body').append(html)
});

And I would suggest using single quotations only unless you need to use escaped characters.

我建议只使用单引号,除非您需要使用转义字符。