javascript 不需要在javascript中转义innerHTML字符?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14413887/
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
no need to escape innerHTML characters in javascript?
提问by wubbewubbewubbe
Should I not escape the double quotes in "markup4" and such? To my surprize this plain works. If I replace them by the " thing it doesn't.
我不应该转义“markup4”等中的双引号吗?令我惊讶的是,这个简单的作品。如果我用 " 替换它们,则不会。
<script type="text/javascript">
function test3(){
document.getElementById('div21').innerHTML += '<div class = "markup4"><br>blablablabla1.<br><br></div><div class = "markup3"><br>blablabla2.<br><br></div>';
}
</script>
回答by wildhaber
These work without escape:
这些工作没有逃脱:
innerHTML = "<div class = 'markup4'>";
innerHTML = '<div class = "markup4">';
Note: When the
"
used outside, the'
works properly inside. (and the opposite)
注意:当在
"
外面使用时,在'
里面正常工作。(和相反)
These need the escape:
这些需要转义:
innerHTML = '<div class = \'markup4\'>';
innerHTML = "<div class = \"markup4\">";
Note: When you use the double quote outside and inside, the inside's
"
needs to be escaped by\
. (same for single quotes)
注意:当您在外部和内部使用双引号时,内部的
"
需要通过\
. (单引号相同)
These will break:
这些将打破:
innerHTML = "<div class = "markup4">";
innerHTML = '<div class = 'markup4'>';
回答by Esailija
First the string is parsed to an internal javascript string representation, so you first need to escape for javascript string literals. The result of this would be the same as what you already have - so no modification is needed.
首先,字符串被解析为内部 javascript 字符串表示,因此您首先需要对 javascript 字符串文字进行转义。这样做的结果将与您已有的结果相同 - 因此无需修改。
Now you have
现在你有
<div class = "markup4"><br>blablablabla1.<br><br></div><div class = "markup3"><br>blablabla2.<br><br></div>
Internally in javascript memory.
在 javascript 内存中。
After this, it's just like you would write that as normal HTML, so of course, if you use "
it will not work.
在此之后,就像您将其编写为普通的 HTML 一样,当然,如果您使用"
它,它将不起作用。
It can get pretty complicated to store strings that go through 2 different interpretations, which is why you shouldn't do this but use templating engines with the templates stored in other files or custom script elements on the page, which would allow you to focus only on the html interpretation.
存储经过 2 种不同解释的字符串可能会变得非常复杂,这就是为什么您不应该这样做,而是使用模板引擎与存储在其他文件或页面上的自定义脚本元素中的模板,这将允许您只关注关于html的解释。
回答by Elracorey
Don't forget browsers render their own innerHtml based on what you give them which can cause problems if nesting innerHtml code.
不要忘记浏览器会根据您提供的内容呈现自己的innerHtml,如果嵌套innerHtml 代码可能会导致问题。
I found the '
and "
codes useful here.
我发现'
和"
代码在这里很有用。
回答by SHANK
escape should be used when you want to include quotes within the data,
当您想在数据中包含引号时应使用转义,
ex: He said, "Please use this!" should get escaped like:
例如:他说,“请用这个!” 应该像这样逃脱:
He said, \" Please use this !\".
他说,\“请用这个!\”。
Otherwise they work without any troubles.
否则,他们可以毫无问题地工作。
Hope I've clarified.
希望我已经澄清了。