IE8 及之前的 javascript/ jquery 焦点问题

发布于 11-28 13:46 字数 821 浏览 0 评论 0原文

我有一个小问题。

目前,我有以下函数用于在输入文本字段的焦点/模糊上格式化货币:

jsfiddle

    $('.number').bind({
        focus: function() {
            $(this).val('$' + $(this).val());
        },
        blur: function() {
            var defVal = $(this)[0].defaultValue; 
            var dollars = $(this).val();
            dollars = dollars.replace('$', '');
            dollars = dollars.replace(',', '');
            dollars = parseFloat(dollars).toFixed(2);
            if (!(isNaN(dollars))) {
            $(this).val(dollars);
            } else {$(this).val(defVal).removeClass('inpt_validBlur');};
        }
    });

这在 Firefox、Safari 和 IE9 中可以正常工作,但在 IE 的早期版本中存在问题。在 IE 中,当您开始输入时,您的文本出现在美元符号之前,而不是之后。有谁知道其原因以及如何解决它?

谢谢

I have a small problem.

currently, I have the following function for formatting currency on the focus/blur of an input text field:

jsfiddle

    $('.number').bind({
        focus: function() {
            $(this).val('

This works just as desired in Firefox, Safari, and IE9, but in prior versions of IE there is an issue. in IE, when you start typing, your text comes BEFORE the dollar sign, not after. Does anyone know the reason for this and how to get around it?

Thank you

+ $(this).val()); }, blur: function() { var defVal = $(this)[0].defaultValue; var dollars = $(this).val(); dollars = dollars.replace('

This works just as desired in Firefox, Safari, and IE9, but in prior versions of IE there is an issue. in IE, when you start typing, your text comes BEFORE the dollar sign, not after. Does anyone know the reason for this and how to get around it?

Thank you

, ''); dollars = dollars.replace(',', ''); dollars = parseFloat(dollars).toFixed(2); if (!(isNaN(dollars))) { $(this).val(dollars); } else {$(this).val(defVal).removeClass('inpt_validBlur');}; } });

This works just as desired in Firefox, Safari, and IE9, but in prior versions of IE there is an issue. in IE, when you start typing, your text comes BEFORE the dollar sign, not after. Does anyone know the reason for this and how to get around it?

Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

執念2024-12-05 13:46:25

这似乎对我有用。并不完美,而且我不太喜欢混合 javascript 和 jquery,但它确实有效。

 $('.inpt').focus(function() {
        var dollars = $(this).val();
        var defVal = $(this)[0].defaultValue;
        $(this).removeClass('inpt').addClass('inpt_Focus');
        if ($(this).val() === defVal || $(this).val() === '
) {
            $(this).val('');
        }
        if ($(this).hasClass('number')) {
            $(this).val('
 + $(this).val());
            var elemLen = $(this).length;
            if (document.selection) {
                var oSel = document.selection.createRange();
                oSel.moveStart('character', -elemLen);
                oSel.moveStart('character', elemLen);
                oSel.moveEnd('character', 0);
                oSel.select();
            }
        }
    });

this seems to be working for me. Not perfect, and I dont really like mixing javascript and jquery, but it works.

 $('.inpt').focus(function() {
        var dollars = $(this).val();
        var defVal = $(this)[0].defaultValue;
        $(this).removeClass('inpt').addClass('inpt_Focus');
        if ($(this).val() === defVal || $(this).val() === '
) {
            $(this).val('');
        }
        if ($(this).hasClass('number')) {
            $(this).val('
 + $(this).val());
            var elemLen = $(this).length;
            if (document.selection) {
                var oSel = document.selection.createRange();
                oSel.moveStart('character', -elemLen);
                oSel.moveStart('character', elemLen);
                oSel.moveEnd('character', 0);
                oSel.select();
            }
        }
    });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文