有关于特殊字符/实体和innerHTML的任何规则吗? [英] Are there any rules regarding special characters/entities and innerHTML?
问题描述
当我从javascript中使用innerHTML时,浏览器(至少是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.
code> \ xa0 (非破坏性空格)将替换为& nbsp;
,
<
(小于)将替换为& lt;
。但是例如,umlautü不会替换为& uuml;
。
The character \xa0
(non breaking space) gets replaced by
,
<
(lesser than) gets replaced by <
. But for example the umlaut ü is not replaced with ü
.
此行为。
一个简单的演示:
<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:
您可以使用gist页面上的raw链接,在浏览器中查看。
You can use the "raw" link on the gist page, to view it in your browser.
我需要知道替换。任何帮助是赞赏。谢谢。
I need to know exactly which characters innerHTML replaces. Any help is appreciated. Thanks.
推荐答案
HTML 5规范有一个规则用于在你调用innerHTML时序列化HTML片段, href =http://www.w3.org/TR/2009/WD-html5-20090212/serializing-html-fragments.html =nofollow noreferrer>序列化HTML片段(请注意innerHTML isn
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屋!