防止Javascript转义文字? [英] Prevent Javascript escaping text?

查看:182
本文介绍了防止Javascript转义文字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

声明

text = '{"section": "\main" }'

产生

"{" section:" main}"

"{"section": "main" }"

有没有一种方法可以防止Javascript解释器将反斜杠当作转义符并将其删除?

Is there a way to prevent the Javascript interpreter from treating the backslash as an escape character and removing it?

我希望能够声明"{" section:" \ main}"并在输出中保留反斜杠"

I want to be able to declare "{"section": "\main" }" and keep the backslash in the output"

"{" section:" \ main}"

"{"section": "\main" }"

注意:我意识到,如果我使用两个反斜杠,它将为我提供所需的输出.但是我不想使用两个反斜杠,因为我在网页中包含很多Latex代码,而且必须对所有内容进行两次转义非常尴尬.那么有没有办法防止Javascript转义文字?

Note: I realise that if I use two backslashes it will give me the desired output. But I don't want to use two backslashes as I have a lot of Latex code that I am including in a webpage and it is extremely awkward having to double escape everything. So is there a way to prevent Javascript escaping text?

推荐答案

console.log(document.getElementById("latextstrings").innerText)

<div id="latextstrings" style="display:none">
  {"section": "\main" }
  {"section": "\sub" }</div>

这篇关于防止Javascript转义文字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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