使用 JQuery 编写时,变音符号在 Web 应用程序中无法正确呈现
我有一个正在本地化的 MVC 4 应用程序。所有内置资源和 Model 属性内容都显示正常,但在我本地化为 JQuery 编写的内容时,编码出现错误。这是事物如何呈现的示例,请注意红色框......它们应该是同一个单词。
问题似乎与我的视图中的以下代码行有关:
$("#FirstName").Watermark("@Resources.ApplicantGivenNameWatermark");
这个JQuery本质上接受一个字符串,然后在没有提供其他值时将其写入输入。以下是该代码的摘要:
$.fn.Watermark = function (text) {
return this.each(
function () {
var input = $(this);
map[map.length] = { text: text, obj: input };
//Similar removal process lies here, this one also removes the style
function insertMessage() {
if (input.val().length == 0 || input.val() == text) {
input.val(text);
input.addClass("watermark");
} else
input.removeClass("watermark")
}
input.focus(clearMessage);
input.blur(insertMessage);
input.change(insertMessage);
insertMessage();
}
);
};
任何有关如何解决此问题的想法将不胜感激。附带说明一下,我已经能够在各种浏览器中可靠地重新创建它。
I have an MVC 4 application that I'm localizing. All the built-in resourcing and Model attributed content is appearing fine, but where I localized content written for JQuery the encoding appears wrong. Here is an example of how things render, note the red boxes... they should be the same word.
The problem appears to revolve around the below line of code in my View:
$("#FirstName").Watermark("@Resources.ApplicantGivenNameWatermark");
This JQuery essentially takes in a string and then writes it to the input when no other value has been supplied. Here is an abstract of that code:
$.fn.Watermark = function (text) {
return this.each(
function () {
var input = $(this);
map[map.length] = { text: text, obj: input };
//Similar removal process lies here, this one also removes the style
function insertMessage() {
if (input.val().length == 0 || input.val() == text) {
input.val(text);
input.addClass("watermark");
} else
input.removeClass("watermark")
}
input.focus(clearMessage);
input.blur(insertMessage);
input.change(insertMessage);
insertMessage();
}
);
};
Any ideas on how to resolve this would be appreciated. As a side note, I've been able to reliably recreate this in a wide range of browsers.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试将其放在函数的开头:
Try to put this at the begin of the function:
问题是 Razor 是为输出 HTML 而构建的,而不是 Javascript。默认情况下,它将使用 HTML 实体对所有字符串进行编码 - 这不适用于 Javascript 字符串。
您可以使用
@Html.Raw(Model.YourString)
,但这会给您带来其他问题,例如不转义引号等(标准 Razor 输出会这样做)因“意外”)。如果您使用的是 .NET 4.0/MVC 3,则可以使用 AjaxHelper 的
JavaScriptStringEncode
,它返回一个IHtmlString
(意味着 Razor 不会对其进行实体编码),但也会正确的 Javascript 特定转义(包括源字符串中的换行符,标准 Razor 编码不会“修复”):(是的,它变成了冗长...但确实具有从根本上解决问题的优点,而不是通过 jQuery 解决它 - 您当然可以创建一个名为
JavaScriptStringEncode
的助手。也是
HttpUtility
上的静态方法 - 从自定义帮助器中调用比创建AjaxHelper
实例更容易,但请注意,与AjaxHelper
不同,该方法在HttpUtility
返回一个简单的字符串,因此您需要将其包装在HtmlString
中以避免 HTML 编码。The problem is that Razor is built for outputting HTML, not Javascript. By default, it will encode all your strings with HTML entities - which aren't appropriate for Javascript strings.
You could use
@Html.Raw(Model.YourString)
, but that will give you other problems, such as not escaping quotation marks etc. (the standard Razor output does that by "accident").If you're using .NET 4.0/MVC 3, you can use AjaxHelper's
JavaScriptStringEncode
, which returns anIHtmlString
(meaning Razor won't entity-encode it) but also does the proper Javascript-specific escaping (including line breaks in the source string, which the standard Razor encoding won't "fix"):(Yeah, it turns rather verbose... but does have the advantage of fixing the problem at its root, rather than working around it through jQuery - you could of course make a helper with a short name which called
JavaScriptStringEncode
.Note that it's also a static method on
HttpUtility
- easier to call from a custom helper than making an instance ofAjaxHelper
, but note that unlikeAjaxHelper
, the method onHttpUtility
returns a simple string, so you'll need to wrap it in anHtmlString
to avoid HTML encoding.