Nib*_*Pig 6 javascript asp.net-mvc-3
我正在使用ASP.NET,我在数据库中有一串HTML.
我想将html变成客户端上的变量.
如果我这样做:
var x = '@Html.Raw(myModel.FishValue)'
Run Code Online (Sandbox Code Playgroud)
它工作正常,因为它基本上是这样做的
var x = '<p>hello!</p>';
Run Code Online (Sandbox Code Playgroud)
但是如果html中有引号则会破坏页面.
我最初的猜测是.替换原始字符串以向引号添加转义,但是两者.ToString()和.ToHtmlString()(因为Html.Raw返回IHtmlString)不会产生与简单相同的标记Html.Raw().
所以我不知道最好做什么.
Cla*_*oom 10
在调用Html.Raw方法之前更换怎么办?
var x = '@Html.Raw(myModel.FishValue.Replace("'","\\'"))'
Run Code Online (Sandbox Code Playgroud)
更新:
来自模型的字符串中可能还有其他转义字符.出于这个原因,我建议首先更换斜杠.当然,这完全取决于模型中服务器的内容.
var x = '@Html.Raw(myModel.FishValue.Replace("\\","\\\\'").Replace("'","\\'"))'
Run Code Online (Sandbox Code Playgroud)
表示javascript中的行为的示例代码段:
//Let's say my Model Content is > I'd Say \ is a escape character. You can't "Escape"
// YOu would have to replace ' --> \' and \ --> \\
var stringFromServer = 'I\'d Say \\ is a escape character. You can\'t "Escape"'
alert(stringFromServer)Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
17027 次 |
| 最近记录: |