有没有一种简单的方法可以在 JavaScript 中将文本转换为 HTML?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10216805/
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-24 00:21:46  来源:igfitidea点击:

Is there an easy way to convert text into HTML in JavaScript?

javascriptjqueryhtml

提问by Dejas

Possible Duplicate:
Escaping HTML strings with jQuery
JavaScript/jQuery HTML Encoding

可能的重复:
使用 jQuery JavaScript/jQuery HTML 编码转义 HTML 字符串

For example, if I wanted to show the user the string x < 3in HTML I would need to replace the <character with &lt;. Is there a prebuilt function to do this in JavaScript or perhaps jQuery that converts any text string into the corresponding HTML?

例如,如果我想向用户显示x < 3HTML 中的字符串,我需要将<字符替换为&lt;. 是否有预先构建的函数可以在 JavaScript 或 jQuery 中执行此操作,将任何文本字符串转换为相应的 HTML?

采纳答案by bhamlin

If you want to use jQuery, you can use the text(string)method.

如果要使用jQuery,可以使用该text(string)方法。

$(".selector").text("x < 5");

http://api.jquery.com/text/

http://api.jquery.com/text/

回答by Starx

Or, Take it simple and do this

或者,简单点,做这个

var str1 = "x < 3";
str1.replace(/</g, '&lt;');

Here is a functionfrom another question

这是另一个问题的函数

function htmlEscape(str) {
    return String(str)
            .replace(/&/g, '&amp;')
            .replace(/"/g, '&quot;')
            .replace(/'/g, '&#39;')
            .replace(/</g, '&lt;')
            .replace(/>/g, '&gt;');
}

Or, Excellent cheat using jQuery Source

或者,使用 jQuery源的优秀作弊

function htmlEncode(value){
  return $('<div/>').text(value).html();
}

function htmlDecode(value){
  return $('<div/>').html(value).text();
}

回答by Salman A

With jQuery perhaps you do not have to html encode your text:

使用 jQuery,也许您不必对文本进行 html 编码:

$("div.test").text("<b></b> for bold!");

sets the inner html of the div to:

将 div 的内部 html 设置为:

&lt;b&gt;&lt;/b&gt; for bold!

You can then retrieve this html using:

然后,您可以使用以下方法检索此 html:

$("div.test").html();

回答by dfsq

You can use jQuery power:

您可以使用 jQuery 功能:

var str = "x < 3";
str = $('<div>').text(str).html();

Will give you "x &lt; 3"result. The string "Alpha & Omega"will become "Alpha &amp; Omega", etc.

会给你"x &lt; 3"结果。字符串"Alpha & Omega"将变为"Alpha &amp; Omega"等。

回答by Code Maverick

If you are using jQuery, you could do as this answer suggests:

如果您使用的是 jQuery,则可以按照此答案的建议进行操作:

HTML-encoding lost when attribute read from input field

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

回答by chepe263

let's say you have stored the string into a variable

假设您已将字符串存储到变量中

k = " <!"#$\'";

then when you need to "print" the values use

然后当您需要“打印”值时使用

escape (k );

回答by beno?t

There is a php function name htmlentities, I found this for JS :

有一个 php 函数名 htmlentities,我为 JS 找到了这个:

function HTMLentities(texte) {

texte = texte.replace(/"/g,'&quot;'); // 34 22
texte = texte.replace(/&/g,'&amp;'); // 38 26
texte = texte.replace(/\'/g,'&#39;'); // 39 27
texte = texte.replace(/</g,'&lt;'); // 60 3C
texte = texte.replace(/>/g,'&gt;'); // 62 3E
texte = texte.replace(/\^/g,'&circ;'); // 94 5E
texte = texte.replace(/‘/g,'&lsquo;'); // 145 91
texte = texte.replace(/'/g,'&rsquo;'); // 146 92
texte = texte.replace(/“/g,'&ldquo;'); // 147 93
texte = texte.replace(/”/g,'&rdquo;'); // 148 94
texte = texte.replace(/?/g,'&bull;'); // 149 95
texte = texte.replace(/–/g,'&ndash;'); // 150 96
texte = texte.replace(/—/g,'&mdash;'); // 151 97
texte = texte.replace(/?/g,'&tilde;'); // 152 98
texte = texte.replace(/?/g,'&trade;'); // 153 99
texte = texte.replace(/?/g,'&scaron;'); // 154 9A
texte = texte.replace(/?/g,'&rsaquo;'); // 155 9B
texte = texte.replace(/?/g,'&oelig;'); // 156 9C
texte = texte.replace(//g,'&#357;'); // 157 9D
texte = texte.replace(/?/g,'&#382;'); // 158 9E
texte = texte.replace(/?/g,'&Yuml;'); // 159 9F
// texte = texte.replace(/ /g,'&nbsp;'); // 160 A0
texte = texte.replace(/?/g,'&iexcl;'); // 161 A1
texte = texte.replace(/¢/g,'&cent;'); // 162 A2
texte = texte.replace(/£/g,'&pound;'); // 163 A3
//texte = texte.replace(/ /g,'&curren;'); // 164 A4
texte = texte.replace(/¥/g,'&yen;'); // 165 A5
texte = texte.replace(/|/g,'&brvbar;'); // 166 A6
texte = texte.replace(/§/g,'&sect;'); // 167 A7
texte = texte.replace(/¨/g,'&uml;'); // 168 A8
texte = texte.replace(/?/g,'&copy;'); // 169 A9
texte = texte.replace(/a/g,'&ordf;'); // 170 AA
texte = texte.replace(/?/g,'&laquo;'); // 171 AB
texte = texte.replace(/?/g,'&not;'); // 172 AC
texte = texte.replace(/-/g,'&shy;'); // 173 AD
texte = texte.replace(/?/g,'&reg;'); // 174 AE
texte = texte.replace(/ˉ/g,'&macr;'); // 175 AF
texte = texte.replace(/°/g,'&deg;'); // 176 B0
texte = texte.replace(/±/g,'&plusmn;'); // 177 B1
texte = texte.replace(/2/g,'&sup2;'); // 178 B2
texte = texte.replace(/3/g,'&sup3;'); // 179 B3
texte = texte.replace(/′/g,'&acute;'); // 180 B4
texte = texte.replace(/μ/g,'&micro;'); // 181 B5
texte = texte.replace(/?/g,'&para'); // 182 B6
texte = texte.replace(/·/g,'&middot;'); // 183 B7
texte = texte.replace(/?/g,'&cedil;'); // 184 B8
texte = texte.replace(/1/g,'&sup1;'); // 185 B9
texte = texte.replace(/o/g,'&ordm;'); // 186 BA
texte = texte.replace(/?/g,'&raquo;'); // 187 BB
texte = texte.replace(/?/g,'&frac14;'); // 188 BC
texte = texte.replace(/?/g,'&frac12;'); // 189 BD
texte = texte.replace(/?/g,'&frac34;'); // 190 BE
texte = texte.replace(/?/g,'&iquest;'); // 191 BF
texte = texte.replace(/à/g,'&Agrave;'); // 192 C0
texte = texte.replace(/á/g,'&Aacute;'); // 193 C1
texte = texte.replace(/?/g,'&Acirc;'); // 194 C2
texte = texte.replace(/?/g,'&Atilde;'); // 195 C3
texte = texte.replace(/?/g,'&Auml;'); // 196 C4
texte = texte.replace(/?/g,'&Aring;'); // 197 C5
texte = texte.replace(/?/g,'&AElig;'); // 198 C6
texte = texte.replace(/?/g,'&Ccedil;'); // 199 C7
texte = texte.replace(/è/g,'&Egrave;'); // 200 C8
texte = texte.replace(/é/g,'&Eacute;'); // 201 C9
texte = texte.replace(/ê/g,'&Ecirc;'); // 202 CA
texte = texte.replace(/?/g,'&Euml;'); // 203 CB
texte = texte.replace(/ì/g,'&Igrave;'); // 204 CC
texte = texte.replace(/í/g,'&Iacute;'); // 205 CD
texte = texte.replace(/?/g,'&Icirc;'); // 206 CE
texte = texte.replace(/?/g,'&Iuml;'); // 207 CF
texte = texte.replace(/D/g,'&ETH;'); // 208 D0
texte = texte.replace(/?/g,'&Ntilde;'); // 209 D1
texte = texte.replace(/ò/g,'&Ograve;'); // 210 D2
texte = texte.replace(/ó/g,'&Oacute;'); // 211 D3
texte = texte.replace(/?/g,'&Ocirc;'); // 212 D4
texte = texte.replace(/?/g,'&Otilde;'); // 213 D5
texte = texte.replace(/?/g,'&Ouml;'); // 214 D6
texte = texte.replace(/×/g,'&times;'); // 215 D7
texte = texte.replace(/?/g,'&Oslash;'); // 216 D8
texte = texte.replace(/ù/g,'&Ugrave;'); // 217 D9
texte = texte.replace(/ú/g,'&Uacute;'); // 218 DA
texte = texte.replace(/?/g,'&Ucirc;'); // 219 DB
texte = texte.replace(/ü/g,'&Uuml;'); // 220 DC
texte = texte.replace(/Y/g,'&Yacute;'); // 221 DD
texte = texte.replace(/T/g,'&THORN;'); // 222 DE
texte = texte.replace(/?/g,'&szlig;'); // 223 DF
texte = texte.replace(/à/g,'&aacute;'); // 224 E0
texte = texte.replace(/á/g,'&aacute;'); // 225 E1
texte = texte.replace(/a/g,'&acirc;'); // 226 E2
texte = texte.replace(/?/g,'&atilde;'); // 227 E3
texte = texte.replace(/?/g,'&auml;'); // 228 E4
texte = texte.replace(/?/g,'&aring;'); // 229 E5
texte = texte.replace(/?/g,'&aelig;'); // 230 E6
texte = texte.replace(/?/g,'&ccedil;'); // 231 E7
texte = texte.replace(/è/g,'&egrave;'); // 232 E8
texte = texte.replace(/é/g,'&eacute;'); // 233 E9
texte = texte.replace(/ê/g,'&ecirc;'); // 234 EA
texte = texte.replace(/?/g,'&euml;'); // 235 EB
texte = texte.replace(/ì/g,'&igrave;'); // 236 EC
texte = texte.replace(/í/g,'&iacute;'); // 237 ED
texte = texte.replace(/?/g,'&icirc;'); // 238 EE
texte = texte.replace(/?/g,'&iuml;'); // 239 EF
texte = texte.replace(/e/g,'&eth;'); // 240 F0
texte = texte.replace(/?/g,'&ntilde;'); // 241 F1
texte = texte.replace(/ò/g,'&ograve;'); // 242 F2
texte = texte.replace(/ó/g,'&oacute;'); // 243 F3
texte = texte.replace(/?/g,'&ocirc;'); // 244 F4
texte = texte.replace(/?/g,'&otilde;'); // 245 F5
texte = texte.replace(/?/g,'&ouml;'); // 246 F6
texte = texte.replace(/÷/g,'&divide;'); // 247 F7
texte = texte.replace(/?/g,'&oslash;'); // 248 F8
texte = texte.replace(/ù/g,'&ugrave;'); // 249 F9
texte = texte.replace(/ú/g,'&uacute;'); // 250 FA
texte = texte.replace(/?/g,'&ucirc;'); // 251 FB
texte = texte.replace(/ü/g,'&uuml;'); // 252 FC
texte = texte.replace(/y/g,'&yacute;'); // 253 FD
texte = texte.replace(/t/g,'&thorn;'); // 254 FE
texte = texte.replace(/?/g,'&yuml;'); // 255 FF
return texte;
}

回答by stUrb

This replaces the characters in javascript:

这将替换 javascript 中的字符:

function replaceStr(str) {
  return str
      .replace(/&/g, "&amp;")
      .replace(/</g, "&lt;")
      .replace(/>/g, "&gt;")
      .replace(/"/g, "&quot;")
      .replace(/'/g, "&#039;");
}

回答by Micha? Miszczyszyn

You could use part of the php.js library which implements functions known from php to js. There's also famous htmlspecialchars: http://phpjs.org/functions/htmlspecialchars:426Obviously, remove unnecessary parts from the function :)

您可以使用 php.js 库的一部分,该库实现了从 php 到 js 的已知功能。还有著名的htmlspecialcharshttp: //phpjs.org/functions/htmlspecialchars: 426显然,从函数中删除不必要的部分:)

Also take a look: HtmlSpecialChars equivalent in Javascript?

也看看:HtmlSpecialChars 等价于 Javascript?