你如何在 JavaScript/jQuery 中转义字符?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8582467/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 06:34:43  来源:igfitidea点击:

How do you escape characters in JavaScript/jQuery?

javascriptjqueryescaping

提问by Jonas

Possible Duplicates:
Escape single quotes in jQuery or JavaScript
Escape dynamic strings in JavaScript

可能的重复:
在 jQuery 或 JavaScript 中转义单引号 在 JavaScript 中
转义动态字符串

How do you assign a string, in this case it's HTML code, to a JavaScript / jQuery variable?

您如何将字符串(在本例中为 HTML 代码)分配给 JavaScript / jQuery 变量?

And how do you escape double quotes and single quotes properly?

以及如何正确转义双引号和单引号?

var html = ""; // <-- HTML goes here

回答by Rodolfo

var html = "<head><title>Hello in \"quotes\"<\/title><\/head>"

var html = "<head><title>Hello in \"quotes\"<\/title><\/head>"