JavaScript:作为字符串一部分的BackSlash [英] JavaScript: A BackSlash as part of the string
问题描述
我有一个JavaScript变量,我使用PHP回显,在页面源代码中显示如下:
I have a JavaScript variable that I echo out using PHP which is shown like this in the page source:
var db_1 = 'C:\this\path';
当我使用该变量设置文本字段的值时,如下所示:
When I set the value of a text field with that variable like so:
$('#myinput').val(db_1);
斜线已经消失,只留下其他字符!
The slashes have disappeared and only the other characters are left!
为什么会这样,我怎么能把斜杠放回去?
Why is this and how can I put the slashes back in??
全部谢谢
推荐答案
反斜杠是JS中的转义字符。解析字符串文字时它们会丢失。
A backslash is an escape character in JS. They are lost when the string literal is parsed.
你不能把它们放回去,因为你无法告诉它们在哪里。你必须确保它们首先保留在字符串中(通过用转义序列表示它们)。
You can't put them back, because you have no way of telling where they were. You have to make sure they remain in the string in the first place (by representing them with an escape sequence).
var db_1 = 'C:\\this\\path';
这篇关于JavaScript:作为字符串一部分的BackSlash的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!