我们有一些带有 onSubmit 的 html:
<form id="login_box" onSubmit="alert('{{=T('Thank you for contacting us! We have received your email and will contact you shortly.')}}'); this.submit(); this.reset(); return false;">
问题是无论我做什么它都不会转义嵌套引号。我试过:
<form id="login_box" onSubmit="alert('{{=T(\'Thank you for contacting us! We have received your email and will contact you shortly.\')}}'); this.submit(); this.reset(); return false;">
<form id="login_box" onSubmit="alert(\"{{=T('Thank you for contacting us! We have received your email and will contact you shortly.')}}\"); this.submit(); this.reset(); return false;">
<form id="login_box" onSubmit=\"alert(\\"{{=T('Thank you for contacting us! We have received your email and will contact you shortly.')}}\\"); this.submit(); this.reset(); return false;\">
以及我能想到的任何其他组合,即使我知道它不会起作用。我没有尝试转义嵌套引号。
最佳答案
我认为您在 python 中使用 twig 或任何类似的模板系统。
如果你想翻译{{=T('message to translate')}}
,只需将该字符串放入自定义属性即可。
示例:
<form id="login_box" msg="{{=T('Thank you for contacting us! We have received your email and will contact you shortly.')}}" onSubmit="alert(this.getAttribute('msg')); this.submit(); this.reset(); return false;">
:)
关于javascript - 为什么这个字符串不会转义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11039212/