javascript 占位符属性在 Internet Explorer 中不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15358240/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 00:31:08  来源:igfitidea点击:

Placeholder attribute not working in internet explorer

javascriptinternet-explorerplaceholder

提问by Patrick Ogbuitepu

The Solution to the issue of placeholders not working in internet explorer is as follows using a combination of javascript and jquery.

使用javascript和jquery的组合,解决占位符在Internet Explorer中不起作用的问题如下。

I must repeat that the Solution to the issue of placeholders not working in internet explorer is as follows using a combination of javascript and jquery.

我必须重复一遍,解决占位符在 Internet Explorer 中不起作用的问题的解决方案如下,使用 javascript 和 jquery 的组合。

    //DETERMINE BROWSER TYPE
    var browser='';
    jQuery.each(jQuery.browser, function(i, val) {
        if(i=='msie')browser=i;
    });

    if(browser=='msie'){
        var password = '<input type="text" value="Password" placeholder="Password" name="q22" class="form-gen-element" id="ie-password-holder" />';

        $(password)
        .insertAfter($('input[name="q2"]'));

        $('input[name="q2"]')
        .hide();

        //INTERNET EXPLORER PLACEHOLDER FIX
        $('#login-content')
        .find('.form-gen-element')
        .focus(function(){
            if($(this).val()==$(this).attr('placeholder')){
                //Move cursor position
                if ($(this).get(0).setSelectionRange) {
                  $(this).get(0).setSelectionRange(0, 0);
                } else if ($(this).get(0).createTextRange) {
                  var range = $(this).get(0).createTextRange();
                  range.collapse(true);
                  range.moveEnd('character', 0);
                  range.moveStart('character', 0);
                  range.select();
                }
            }
        })
        .blur(function(){
            if($(this).val()==''){
                $(this).val($(this).attr('placeholder'));

                if($(this).attr('name')=='q2')
                    $(this).attr('type','text');
            }
        })
        .keydown(function(e){
            if($(this).val()==$(this).attr('placeholder')){
                if((e.keyCode > 47 && e.keyCode < 91) || e.keyCode==109 || e.keyCode==110 || e.keyCode==190 || e.keyCode==110){
                    var t = $(this).val().split($(this).attr('placeholder'));
                    $(this).val(t[0]);

                    //Handle password attribute change
                    if($(this).attr('name')=='q22'){
                        $(this).attr('type','password');
                    }
                }
            }
        });

    }else{
        //OTHER BROWSERS DEFAULT FUNCTIONALITY
        $('#login-content')
        .find('.form-gen-element')
        .val('');
    }
});

回答by The Reader

View the below helpful question of SO
Input placeholders for Internet Explorer

查看以下有关 Internet Explorer的 SO输入占位符的有用问题