有没有一种简单的方法可以在 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
Is there an easy way to convert text into HTML in JavaScript?
提问by Dejas
Possible Duplicate:
Escaping HTML strings with jQuery
JavaScript/jQuery HTML Encoding
For example, if I wanted to show the user the string x < 3
in HTML I would need to replace the <
character with <
. Is there a prebuilt function to do this in JavaScript or perhaps jQuery that converts any text string into the corresponding HTML?
例如,如果我想向用户显示x < 3
HTML 中的字符串,我需要将<
字符替换为<
. 是否有预先构建的函数可以在 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");
回答by Starx
Or, Take it simple and do this
或者,简单点,做这个
var str1 = "x < 3";
str1.replace(/</g, '<');
Here is a functionfrom another question
这是另一个问题的函数
function htmlEscape(str) {
return String(str)
.replace(/&/g, '&')
.replace(/"/g, '"')
.replace(/'/g, ''')
.replace(/</g, '<')
.replace(/>/g, '>');
}
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 设置为:
<b></b> 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 < 3"
result. The string "Alpha & Omega"
will become "Alpha & Omega"
, etc.
会给你"x < 3"
结果。字符串"Alpha & Omega"
将变为"Alpha & Omega"
等。
回答by Code Maverick
If you are using jQuery, you could do as this answer suggests:
如果您使用的是 jQuery,则可以按照此答案的建议进行操作:
回答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,'"'); // 34 22
texte = texte.replace(/&/g,'&'); // 38 26
texte = texte.replace(/\'/g,'''); // 39 27
texte = texte.replace(/</g,'<'); // 60 3C
texte = texte.replace(/>/g,'>'); // 62 3E
texte = texte.replace(/\^/g,'ˆ'); // 94 5E
texte = texte.replace(/‘/g,'‘'); // 145 91
texte = texte.replace(/'/g,'’'); // 146 92
texte = texte.replace(/“/g,'“'); // 147 93
texte = texte.replace(/”/g,'”'); // 148 94
texte = texte.replace(/?/g,'•'); // 149 95
texte = texte.replace(/–/g,'–'); // 150 96
texte = texte.replace(/—/g,'—'); // 151 97
texte = texte.replace(/?/g,'˜'); // 152 98
texte = texte.replace(/?/g,'™'); // 153 99
texte = texte.replace(/?/g,'š'); // 154 9A
texte = texte.replace(/?/g,'›'); // 155 9B
texte = texte.replace(/?/g,'œ'); // 156 9C
texte = texte.replace(//g,'ť'); // 157 9D
texte = texte.replace(/?/g,'ž'); // 158 9E
texte = texte.replace(/?/g,'Ÿ'); // 159 9F
// texte = texte.replace(/ /g,' '); // 160 A0
texte = texte.replace(/?/g,'¡'); // 161 A1
texte = texte.replace(/¢/g,'¢'); // 162 A2
texte = texte.replace(/£/g,'£'); // 163 A3
//texte = texte.replace(/ /g,'¤'); // 164 A4
texte = texte.replace(/¥/g,'¥'); // 165 A5
texte = texte.replace(/|/g,'¦'); // 166 A6
texte = texte.replace(/§/g,'§'); // 167 A7
texte = texte.replace(/¨/g,'¨'); // 168 A8
texte = texte.replace(/?/g,'©'); // 169 A9
texte = texte.replace(/a/g,'ª'); // 170 AA
texte = texte.replace(/?/g,'«'); // 171 AB
texte = texte.replace(/?/g,'¬'); // 172 AC
texte = texte.replace(/-/g,'­'); // 173 AD
texte = texte.replace(/?/g,'®'); // 174 AE
texte = texte.replace(/ˉ/g,'¯'); // 175 AF
texte = texte.replace(/°/g,'°'); // 176 B0
texte = texte.replace(/±/g,'±'); // 177 B1
texte = texte.replace(/2/g,'²'); // 178 B2
texte = texte.replace(/3/g,'³'); // 179 B3
texte = texte.replace(/′/g,'´'); // 180 B4
texte = texte.replace(/μ/g,'µ'); // 181 B5
texte = texte.replace(/?/g,'¶'); // 182 B6
texte = texte.replace(/·/g,'·'); // 183 B7
texte = texte.replace(/?/g,'¸'); // 184 B8
texte = texte.replace(/1/g,'¹'); // 185 B9
texte = texte.replace(/o/g,'º'); // 186 BA
texte = texte.replace(/?/g,'»'); // 187 BB
texte = texte.replace(/?/g,'¼'); // 188 BC
texte = texte.replace(/?/g,'½'); // 189 BD
texte = texte.replace(/?/g,'¾'); // 190 BE
texte = texte.replace(/?/g,'¿'); // 191 BF
texte = texte.replace(/à/g,'À'); // 192 C0
texte = texte.replace(/á/g,'Á'); // 193 C1
texte = texte.replace(/?/g,'Â'); // 194 C2
texte = texte.replace(/?/g,'Ã'); // 195 C3
texte = texte.replace(/?/g,'Ä'); // 196 C4
texte = texte.replace(/?/g,'Å'); // 197 C5
texte = texte.replace(/?/g,'Æ'); // 198 C6
texte = texte.replace(/?/g,'Ç'); // 199 C7
texte = texte.replace(/è/g,'È'); // 200 C8
texte = texte.replace(/é/g,'É'); // 201 C9
texte = texte.replace(/ê/g,'Ê'); // 202 CA
texte = texte.replace(/?/g,'Ë'); // 203 CB
texte = texte.replace(/ì/g,'Ì'); // 204 CC
texte = texte.replace(/í/g,'Í'); // 205 CD
texte = texte.replace(/?/g,'Î'); // 206 CE
texte = texte.replace(/?/g,'Ï'); // 207 CF
texte = texte.replace(/D/g,'Ð'); // 208 D0
texte = texte.replace(/?/g,'Ñ'); // 209 D1
texte = texte.replace(/ò/g,'Ò'); // 210 D2
texte = texte.replace(/ó/g,'Ó'); // 211 D3
texte = texte.replace(/?/g,'Ô'); // 212 D4
texte = texte.replace(/?/g,'Õ'); // 213 D5
texte = texte.replace(/?/g,'Ö'); // 214 D6
texte = texte.replace(/×/g,'×'); // 215 D7
texte = texte.replace(/?/g,'Ø'); // 216 D8
texte = texte.replace(/ù/g,'Ù'); // 217 D9
texte = texte.replace(/ú/g,'Ú'); // 218 DA
texte = texte.replace(/?/g,'Û'); // 219 DB
texte = texte.replace(/ü/g,'Ü'); // 220 DC
texte = texte.replace(/Y/g,'Ý'); // 221 DD
texte = texte.replace(/T/g,'Þ'); // 222 DE
texte = texte.replace(/?/g,'ß'); // 223 DF
texte = texte.replace(/à/g,'á'); // 224 E0
texte = texte.replace(/á/g,'á'); // 225 E1
texte = texte.replace(/a/g,'â'); // 226 E2
texte = texte.replace(/?/g,'ã'); // 227 E3
texte = texte.replace(/?/g,'ä'); // 228 E4
texte = texte.replace(/?/g,'å'); // 229 E5
texte = texte.replace(/?/g,'æ'); // 230 E6
texte = texte.replace(/?/g,'ç'); // 231 E7
texte = texte.replace(/è/g,'è'); // 232 E8
texte = texte.replace(/é/g,'é'); // 233 E9
texte = texte.replace(/ê/g,'ê'); // 234 EA
texte = texte.replace(/?/g,'ë'); // 235 EB
texte = texte.replace(/ì/g,'ì'); // 236 EC
texte = texte.replace(/í/g,'í'); // 237 ED
texte = texte.replace(/?/g,'î'); // 238 EE
texte = texte.replace(/?/g,'ï'); // 239 EF
texte = texte.replace(/e/g,'ð'); // 240 F0
texte = texte.replace(/?/g,'ñ'); // 241 F1
texte = texte.replace(/ò/g,'ò'); // 242 F2
texte = texte.replace(/ó/g,'ó'); // 243 F3
texte = texte.replace(/?/g,'ô'); // 244 F4
texte = texte.replace(/?/g,'õ'); // 245 F5
texte = texte.replace(/?/g,'ö'); // 246 F6
texte = texte.replace(/÷/g,'÷'); // 247 F7
texte = texte.replace(/?/g,'ø'); // 248 F8
texte = texte.replace(/ù/g,'ù'); // 249 F9
texte = texte.replace(/ú/g,'ú'); // 250 FA
texte = texte.replace(/?/g,'û'); // 251 FB
texte = texte.replace(/ü/g,'ü'); // 252 FC
texte = texte.replace(/y/g,'ý'); // 253 FD
texte = texte.replace(/t/g,'þ'); // 254 FE
texte = texte.replace(/?/g,'ÿ'); // 255 FF
return texte;
}
回答by stUrb
This replaces the characters in javascript:
这将替换 javascript 中的字符:
function replaceStr(str) {
return str
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, """)
.replace(/'/g, "'");
}
回答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 的已知功能。还有著名的htmlspecialchars
:http: //phpjs.org/functions/htmlspecialchars: 426显然,从函数中删除不必要的部分:)
Also take a look: HtmlSpecialChars equivalent in Javascript?