使用 JQuery 编写时,变音符号在 Web 应用程序中无法正确呈现

发布于 2025-01-03 15:19:19 字数 1206 浏览 4 评论 0原文

我有一个正在本地化的 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.

enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

流殇 2025-01-10 15:19:20

尝试将其放在函数的开头:

text=$('<span>'+text+'</span>').text();

Try to put this at the begin of the function:

text=$('<span>'+text+'</span>').text();
会傲 2025-01-10 15:19:20

问题是 Razor 是为输出 HTML 而构建的,而不是 Javascript。默认情况下,它将使用 HTML 实体对所有字符串进行编码 - 这不适用于 Javascript 字符串。

可以使用@Html.Raw(Model.YourString),但这会给您带来其他问题,例如不转义引号等(标准 Razor 输出会这样做)因“意外”)。

如果您使用的是 .NET 4.0/MVC 3,则可以使用 AjaxHelper 的 JavaScriptStringEncode,它返回一个 IHtmlString(意味着 Razor 不会对其进行实体编码),但也会正确的 Javascript 特定转义(包括源字符串中的换行符,标准 Razor 编码不会“修复”):(

$("#FirstName").Watermark(
   "@Ajax.JavaScriptStringEncode(Resources.ApplicantGivenNameWatermark")");

是的,它变成了冗长...但确实具有从根本上解决问题的优点,而不是通过 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 an IHtmlString (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"):

$("#FirstName").Watermark(
   "@Ajax.JavaScriptStringEncode(Resources.ApplicantGivenNameWatermark")");

(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 of AjaxHelper, but note that unlike AjaxHelper, the method on HttpUtility returns a simple string, so you'll need to wrap it in an HtmlString to avoid HTML encoding.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文