是否有对特殊字符/实体和innerHTML的任何规则? [英] Are there any rules regarding special characters/entities and innerHTML?
问题描述
当我使用的innerHTML从JavaScript,浏览器(Safari和Firefox至少)似乎与他们的命名实体,以取代某些字符和数字实体。
When I use innerHTML from javascript, the browser (Safari and Firefox at least) seem to replace certain characters and numeric entities with their named entities.
字符 \\ XA0
(不换行空格)获取&放替换
,
&放大器;#60;
(比小)得到由&放大器更换; LT;
。但是,例如变音ü没有与替换&放大器; uuml;
The character \xa0
(non breaking space) gets replaced by
,
<
(lesser than) gets replaced by <
. But for example the umlaut ü is not replaced with ü
.
我还没有发现这种行为的任何文档。
I have not found any documentation for this behaviour.
一个简单的演示:
<h2 id="withoutnbsp">This does not use en be es pe (uses the \xa0 character)</h2>
<script>alert(document.getElementById("withoutnbsp").innerHTML);</script>
一个更复杂的演示可以在这里找到:
A more elaborate demonstration can be found here:
您可以使用要点页面上的原始的链接,在浏览器中查看。
You can use the "raw" link on the gist page, to view it in your browser.
我需要确切的innerHTML替换的字符知道。任何帮助是AP preciated。谢谢你。
I need to know exactly which characters innerHTML replaces. Any help is appreciated. Thanks.
推荐答案
的HTML 5规范对序列化HTML片段规则,当你打电话的innerHTML就可以了,这里记载:的序列化的HTML片段的(注意innerHTML的不是在HTML 4规范,所以在HTML 5规范是当前参考)。
The HTML 5 spec has rules for serializing an HTML fragment when you call innerHTML on it, documented here: Serializing HTML fragments (note that innerHTML isn't in the HTML 4 spec, so the HTML 5 spec is the current reference).
规范的TLDR版本:空间,尖括号,&号,等号与双引号时的innerHTML被称为是那些获得逃脱的唯一字符
TLDR version of the spec: Spaces, angle brackets, ampersands, equals signs and double quotes are the only chars that get escaped when innerHTML is called.
这篇关于是否有对特殊字符/实体和innerHTML的任何规则?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!