---
title: "javascript 转义字符使用"
author: "henu_zhangyang"
date: "2015-01-14T13:13:42Z"
tags: []
categories: ["Web前端"]
slug: "javascript"
draft: false
lastmod: "2015-01-14T13:15:57Z"
id: 2175795
url: "https://ihenu.iteye.com/blog/2175795"
---

转义字符使用场合如下部分：

```
        AV.Cloud.run("practices", {type: 'get_practices',class_id:class_id}, {
            success: function(data){
                data.forEach(function(grade){
                    //此位置渲染图片
                    var grade_title = grade.grade;
                    grade.content.forEach(function(content,index){
                        $(".unit").html('<ul class="unit01"><li class="unit_title"><a href="javascript:void(0);" onclick="go_word_read(\''
                        + content.unit + '\',\'' + grade_title + '\')">'+ content.unit +'</a></li><li class="unit_text">'+ content.title+ '</li></ul>');
//                        console.log($('.unit').html());
                    })
                });

            },
            error: function(err){
                console.log(err);
            }
        });
 
```

 

 程序在拼装html元素时，最外层是有 **‘    ’** 来包起来的，它代表一个字符串，但是当我们在里面的函数可能也需要使用 ' ' ，如果不使用转义字符的话，浏览器在解析到第二个  '  的时候  ，会认为该字符串已经完成，所以不能达到我们所要的效果，因此这种情况下，我们就需要使用转义字符了，在需要使用 ‘  而不代表结束标示时，我们可以使用 \\'进行转义，这样程序就不会认为该字符串在此处结束。

类似的专业字符还有很多，比如  \\ '    \\"     \\& ;不再一一介绍。