leohxj
1/26/2016 - 7:13 AM

supportPlaceholder.js

supportPlaceholder.js

var supportPlaceHolder = function() {
    // first, detect the placeholder attribute
    var isPlaceholder = function(){
        var input = document.createElement('input');
        return 'placeholder' in input;
    }

    if(!isPlaceholder()){
        $('[placeholder]').each(function(){
            var defaultValue = $(this).attr("placeholder");

            if($.trim($(this).val())==""){
                $(this).val(defaultValue).css("color","#999");
            }

            $(this).focus(function(){
                var defaultValue = $(this).attr("placeholder");
                if($.trim($(this).val())==defaultValue){
                    $(this).val("").css("color","#333");
                }
            }).blur(function(){
                var defaultValue = $(this).attr("placeholder");
                if($.trim($(this).val())==""){
                    $(this).val(defaultValue).css("color","#999")
                }
            });
        })
    }
}