I doubt the last option, but probably one of the first two. Can anybody tell me which?
I'm getting the error in the screen shot.
<html>
<head>
<script type="text/javascript">
var html = "<script></script>";
</script>
</head>
<body>
</body>
</html>
I doubt the last option, but probably one of the first two. Can anybody tell me which?
I'm getting the error in the screen shot.
<html>
<head>
<script type="text/javascript">
var html = "<script></script>";
</script>
</head>
<body>
</body>
</html>
这在每个浏览器中都是一个问题,脚本块在第一个字符串处终止</script>
,因此如果该字符串出现在您的代码中的任何位置,它将导致脚本块过早终止。
如果您想在 JS 中将此作为变量,请使用:
var html = unescape("%3Cscript%3E%3C/script%3E");
您还可以使用 \ 正确呈现该字符:
var html = "<script><\/script>";
那不是错误。这是脚本标签的正确行为,您所做的相当于不转义字符串中的引号。
var string = 'My mother's awesome.';
解决问题的一种简单方法是拆分</script>
标签,如下所示:
var html = "<script></"+"script>";