2

环境:Visual Studio 2008 SP1、ASP.NET 和 JavaScript

我正在尝试进行快速document.write测试,但是一旦我添加了脚本标签,Visual Studio 编辑器就会不喜欢它。具体来说,结束脚本标签?我一输入结束</script>标签就得到那些波浪线

<html><head></head><body>
    <script type='text/javascript'>
      document.write('<html><head><script></script></head><body></body></html>');
    </script>
</body>
</html>
4

2 回答 2

3

利用:

document.write('<html><head><script><\/script></head><body></body></html>');
Note the `\`, here ------------------^

另一种选择是拆分字符串,使其不是一个精确的标签:

document.write('<html><head><script></sc' + 'ript></head><body></body></html>');

这是一个解释为什么需要它的参考,以及其他选项:

无论如何,您为什么要将整个文档写入<body>?

于 2013-04-17T16:58:02.420 回答
3

我对 Visual Studio 不熟悉,但我可以看出您的 HTML 体系结构存在一些错误,如果它已经在 HTML 文档中,则不应重新插入<html><head><body>,也尝试转义斜杠:

<html><head></head><body>
    <script type='text/javascript'>
      document.write('<script>alert("hello");<\/script>');
    </script>
</body>
</html>

如果 alert hello 有效,那么你就在良好的轨道上

于 2013-04-17T17:03:21.050 回答