Placeholder attribute not supported in IE. Any suggestions?

后端 未结 6 2097
走了就别回头了
走了就别回头了 2020-12-30 02:22

What do you all use to support placeholder attributes in browsers?

Currently, am using:

https://github.com/mathiasbynens/Placeholder-jQuery-Plugin

Ha

6条回答
  •  没有蜡笔的小新
    2020-12-30 02:44

    This is what I used in one of my projects. The only thing is that I only needed a placeholder on one element, so it might not work for your situation right away. But just to get the idea of how simple the code is:

    (function(){
        var nameInput = document.getElementById('your-id-here');
        var placeHolderSupport = ("placeholder" in document.createElement("input"));
        if( !placeHolderSupport )
        {
            //show hint in gray
            var placeholder = nameInput.getAttribute('placeholder');
            nameInput.onfocus = function(){ if(this.value==placeholder){this.value='';this.className='';} };
            nameInput.onblur  = function(){ if(this.value==''){this.value=placeholder;this.className='placeholder';} };
            nameInput.onblur();
        }
    })()
    

提交回复
热议问题