为什么js不能理解字符串'< / script>'? [英] Why js can't understand string '</script>'?

查看:124
本文介绍了为什么js不能理解字符串'< / script>'?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的JavaScript (jsFiddle)

 警报( '< /脚本>'); 

浏览器无法理解它。

这是控制台输出:

 未捕获的SyntaxError:意外的标记非法

但是这个脚本的工作原理是(jsFiddle)

  alert('< / scriptt>'); //显示提醒文字'< / scriptt>'

这是某种浏览器错误还是普通的 ECMAScript 行为? p>

(浏览器是Chrome) 解决方案

/ p>

 < script> 
alert('
< / script>
');

这是 SyntaxError



您可以使用

  alert('< \ / script> \ N'); 


I have a simple javascript (jsFiddle):

alert('</script>');

Browser fails to understand it.
This is console output:

Uncaught SyntaxError: Unexpected token ILLEGAL 

But this script works (jsFiddle):

alert('</scriptt>');//shows alert text '</scriptt>'

Is it some kind of browser bug or normal ECMAScript behaviour?

(browser is Chrome)

解决方案

Because it is considered as:

<script>
alert('
</script>
');

which is a SyntaxError

You can use

alert( '<\/script>\n');

这篇关于为什么js不能理解字符串'&lt; / script&gt;'?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆