更新时间:2023-09-29 22:19:16
HTML在之前解析并独立于Javascript。当前的浏览器行为是,一旦找到打开的标签< script>
,浏览器将切换到脚本数据状态并将所有后续数据解释为脚本,直到找到< / script>
。
HTML is parsed before and independent from Javascript. The current browser behavior is that, once an open tag <script>
is found, the browser will switch to "Script Data State" and interpret all following data as script until a </script>
is found.
< / script> 无关紧要 - 在JS字符串,JS注释,CDATA部分甚至HTML注释中。
Where the </script>
is detected doesn't matter — inside a JS string, a JS comment, a CDATA section, or even HTML comment.
你需要制作字符串看起来不像< / script>
到HTML解析器。最简单的方法是写 https://***.com/questions/3509818/why-script-tag -in-js-string-is-being-validated / 3509843#3509843> @ Daniel 的回答。
You need to make the string does not look like </script>
to the HTML parser. The simplest way is to write <\/script>
as in @Daniel's answer.