字符串.rawInternet Explorer中的等效项?

2024-05-29 00:18:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我返回一个使用Python的Zope生成的DTML模板,并通过在浏览器中对其求值来为其分配一个JS变量。我的模板看起来像:

var html = <dtml-var "html">

# which is nothing but
var html = "<html>
<body>
Hello
</body>
</html>
"

这是无效的,因为Javascript要求在换行符前加上双引号,如JavaScript string with new line - but not using \n等问题中所述。在

我想我真的可以:

^{pr2}$

它在Chrome中工作,但是String.rawIE 11中不存在。 我有没有别的办法可以用?在

或者,我可以通过下面的代码片段在Python中解决这个问题吗?有什么注意事项吗?在

# Replace newline character to \n literally.
html.replace("\n",\\n")

# Escape the double quotes
html.replace("\n",\\n").replace("\"", "\\\"") i.e replace " with \"

Tags: 模板zopewhichisvarhtmlwithjs

热门问题