我返回一个使用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.raw
在IE 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 \"
目前没有回答
相关问题 更多 >
编程相关推荐