Placeholder attribute on input tags for IE?

前端 未结 6 903
你的背包
你的背包 2021-01-04 03:30

I know that Internet Explorer doesn\'t support the placeholder attribute for input tags, but surely in 2012 there must be another solution for IE?

6条回答
  •  走了就别回头了
    2021-01-04 04:16

    Yes, there is a quite easy solution for IE8 and IE9 because on grater versions of IE it already works. (IE10, IE11, etc)

    This is the solution i found:

    1. Detect Internet Explorer version

         
      
    

    2. Fix the placeholder

    if (typeof ie == 'undefined') var ie = 10;   
    if (ie == 8 || ie == 9){  
    
        $("input[placeholder]").each(function() {
            this.value = $(this).attr('placeholder');
        });        
    
        $("input[placeholder]").focus(function() 
            if (this.value == $(this).attr('placeholder')) this.value = '';
        }).blur(function() {   
            if (this.value == '')
                this.value = $(this).attr('placeholder'); 
        });
    }
    

提交回复
热议问题