简体   繁体   English

在 JavaScript 中输出 HTML 实体

[英]Outputting HTML entities in JavaScript

I'm struggling with the simple syntax of outputting HTML entities.我正在为输出 HTML 实体的简单语法而苦苦挣扎。

For the sake of simplicity:为了简单起见:

document.write("é");

I want to output é我想输出é as é .作为é

I've tried it with quotation marks, apostrophes, and no marks.我已经尝试过使用引号、撇号和无标记。

The only way I've managed to get it to work is /é/ but that outputs /é/ .我设法让它工作的唯一方法是/é/但输出/é/

Can somebody inform me of the correct way of doing this ( and if possible explain why the slashes are doing their thing )?有人可以告诉我这样做的正确方法吗(如果可能,请解释为什么斜线在做他们的事情)?

I'm not familiar with jQuery so I'd appreciate if you keep answers in core JavaScript.我不熟悉 jQuery,所以如果您在核心 JavaScript 中保留答案,我将不胜感激。

https://jsfiddle.net/e2ezz2h1/ https://jsfiddle.net/e2ezz2h1/

Based on this answer: https://stackoverflow.com/a/9609450/2637098 and the additional information you provided, this code should suit your needs.根据此答案: https : //stackoverflow.com/a/9609450/2637098以及您提供的其他信息,此代码应该适合您的需求。

 var element = document.createElement('div'); element.innerHTML = "Héllo world!"; document.getElementById('demo1').value = element.textContent; element.textContent = '';
 <input type="text" id="demo1" />

You have to escape the ampersand with &amp;你必须用&amp;来逃避&amp; :

 document.write("&amp;eacute;");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM