简体   繁体   English

IE9 JavaScript问题的占位符脚本

[英]Placeholder script for IE9 javascript issue

I have the following script which if the users has the browser ie9 then we fix the known issue regarding ie9 not supporting place holders: 我有以下脚本,如果用户使用的是浏览器ie9,那么我们可以修复有关ie9不支持占位符的已知问题:

       if (isIE9) { // ie9
            // this is html5 placeholder fix for inputs, inputs with placeholder-no-fix class will be skipped(e.g: we need this for password fields)
            jQuery('input[placeholder]:not(.placeholder-no-fix), textarea[placeholder]:not(.placeholder-no-fix)').each(function() {

                var input = jQuery(this);

                if (input.val() == '' && input.attr("placeholder") != '') {
                    input.addClass("placeholder").val(input.attr('placeholder'));
                }

                input.focus(function() {
                    if (input.val() == input.attr('placeholder')) {
                        input.val('');
                    }
                });

                input.blur(function() {
                    if (input.val() == '' || input.val() == input.attr('placeholder')) {
                        input.val(input.attr('placeholder'));
                    }
                });
            });
        }

When I load the page in ie9 I see the place holder for the email box, but the password box has decided to take the placeholder and make it look like someone has entered a value as shown here: 当我在ie9中加载页面时,我看到了电子邮件框的占位符,但是密码框决定使用该占位符,并使其看起来像有人输入了一个值,如下所示:

在此处输入图片说明

My markup is as follows: 我的标记如下:

<div class="form-group">
   <input id="EmailAddress" class="form-control" type="text" value="" placeholder="Please enter your email address" name="EmailAddress" data-val-required="Email Address is required" data-val="true" autocomplete="off" maxlength="320">
</div>
<div class="form-group">
   <input id="Password" class="form-control" type="Password" value="" placeholder="Password" name="Password" data-val-required="Password is required" data-val="true" autocomplete="off" maxlength="50">
</div>

Can anyone suggest anything that might help me cure the problem? 有人可以提出任何可以帮助我解决问题的建议吗?

I also added placeholder-no-fix to the password class but yet when viewed in ie9 the password field is empty it does not show the place holder. 我还向密码类添加了占位符-不固定,但是在ie9中查看时,密码字段为空,它不会显示占位符。

You're setting the value (.val()) of the password field to the string "Password", which it treats as a password and hides the characters. 您正在将密码字段的值(.val())设置为字符串“ Password”,将其视为密码并隐藏字符。

I suppose as a dirty-ish hack you could set the input type to text until they focus the field, then set it to password until the field is empty on blur again (it'd obviously be bad to set it to text if their password was in there!). 我想作为一个肮脏的黑客,您可以将输入类型设置为文本,直到他们专注于该字段,然后将其设置为密码,直到再次模糊时该字段为空(如果使用他们的密码,将其设置为文本显然很糟糕在那里!)。

I recommend you using an extra position: absolute; 我建议您使用一个额外的职位:绝对职位; div as the placeholder fallback for IE 9. Simply show / hide the div onblur. div作为IE 9的占位符后备。只需显示/隐藏div onblur。

Using value as placeholder would require the user to delete everything before filling in the fields, and it would definitely have the ****** issue in the password field. 使用值作为占位符将要求用户在填写字段之前删除所有内容,并且在密码字段中肯定会出现******问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM