placeholder is not working in IE9

不打扰是莪最后的温柔 提交于 2019-12-01 08:07:46

问题


I am salesforce (SFDC) developer. In my visualforce page for input box I am using placeholder code.

<div class="control-group">
    <label class="control-label visible-desktop" for="first_name">First Name</label>
    <div class="controls">
        <input class="input-block-level" name="First Name" id="first_name" placeholder="First Name" value="" type="text" required="required" autofocus="autofocus" />
    </div>
</div>

I checked in internet for some CSS hack but I didn't find any. I find some javascript hack.

HTML5 Placeholder jQuery Plugin

https://github.com/mathiasbynens/jquery-placeholder

Demo & Examples

http://mathiasbynens.be/demo/placeholder

But I don't want to use jQuery hack or something.


回答1:


As IE9 doesn't support the placeholder attribute, you can do it in Javascript/jQuery like so (quickly written, not tested):

if(navigator.appVersion.match(/MSIE [\d.]+/)){
    var placeholderText = 'Some Placeholder Text';
    $('#first_name').val(placeholderText);
    $('#first_name').blur(function(){
        $(this).val() == '' ? $(this).val(placeholderText) : false;
    });
    $('#first_name').focus(function(){
        $(this).val() == placeholderText ? $(this).val('') : false;
    });
}

Do the same for the blur event too, then that will mimic a placeholder attribute.

[Edit]

Okay, after rethinking this (due to the comment) this is really not the most elegant solution (however it does work), so I would disregard this answer totally.




回答2:


if(navigator.appVersion.match(/MSIE [\d.]+/)){
    $(document).find("input[placeholder]").each(function(){
        if($.trim($(this).val()) == ""){
            $(this).val($(this).attr("placeholder")).addClass('placeholder');
        }
        $(this).on("focus",function(){
            $(this).hasClass('placeholder') ? $(this).val('').removeClass('placeholder') : false;
        }).on("blur",function(){
            $(this).val() == '' ? $(this).val($(this).attr("placeholder")).addClass('placeholder') :false;          
        });
    });     
}



回答3:


A little simpler answer worked for me not being very trusting of Regex (my downfall)

function setPlaceHolderForIE9() {
    var pos = window.navigator.userAgent.indexOf("MSIE");

    if (pos > 0) {
        if (window.navigator.userAgent.substring(pos + 5, window.navigator.userAgent.indexOf(".", pos)) < 10) {
            //alert($("input[placeholder]").val($("input[placeholder]").attr("placeholder")));
            $("input[placeholder]").each(function () {
                $(this).val($(this).attr("placeholder"));
            });

            $("input[placeholder]").click(function () {
                if ($(this).val() === $(this).attr("placeholder")) {
                    $(this).val('');
                }
            });

            $('input[placeholder]').blur(function () {

                if ($.trim($(this).val()).length === 0) {
                    $(this).val($(this).attr("placeholder"));
                }
            });


        }
    }
}


来源:https://stackoverflow.com/questions/17696646/placeholder-is-not-working-in-ie9

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!