js中\转义符

<!DOCTYPE html>
<html lang="zh-cn">

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style></style>
</head>

<body>
    <h1>Head</h1>
    <script>
        document.write('<script>alert(123) <\/script>'); //这里同理是让js解析的时候再识别括号里面的script标签,这样就避免了括号里面的script标签和最上的的script标签形成一对引起的错误
        var txt = "We are the so-called \"Vikings\" from the north."
        var txt2 = "We are the so-called &quot;Vikings&quot; from the north."
        document.write(txt); //We are the so-called "Vikings" from the north.
        document.write(txt2); //We are the so-called "Vikings" from the north.
        console.log(txt); //We are the so-called "Vikings" from the north. \是被js转义,不被浏览器转义
        console.log(txt2); //We are the so-called &quot;Vikings&quot; from the north. &quot被浏览器转义,不被js转义
    </script>
    <h2>Tail</h2>
</body>

</html>

http://c.biancheng.net/view/5385.html


版权声明:本文为weixin_48079602原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。