Solve the problem that IE does not support placeholder

【Foreword】

     In practice, it is found that the effect of jquery.placeholder.js on placeholder support is not particularly ideal, and the value of the placeholder attribute disappears from time to time, so it is recommended to use the native JS solution.

 

【Code】

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title> demo measurement </ title>
	<style type="text/css">
		*{margin: 0;padding: 0}
	</style>
</head>
<body>
<input type="text" name="username" placeholder="请输入名字">
<script type="text/javascript">
	placeholder();
	function placeholder() {  
    var doc=document,  
        inputs=doc.getElementsByTagName('input'),  
        //Whether the browser input field supports placeholder  
        supportPlaceholder='placeholder'in doc.createElement('input'),  
        placeholder=function(input){  
            var text=input.getAttribute('placeholder'),  
            defaultValue=input.defaultValue;  
            if(defaultValue==''){  
                input.value=text  
            }  
            input.onfocus=function(){  
                if(input.value===text){  
                    this.value=''  
                }  
            };  
            input.onblur=function(){  
                if(input.value===''){  
                    this.value=text  
                }  
            }  
        };  
        if(!supportPlaceholder){  
            for(var i=0,len=inputs.length;i<len;i++){  
                var input=inputs[i],  
                text=input.getAttribute('placeholder');  
                if(input.type==='text'&&text){  
                    placeholder(input)  
                }  
            }  
        }  
}  	
</script>
</body>
</html>

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326104721&siteId=291194637