zoukankan      html  css  js  c++  java
  • HTML5之placeholder的优雅解决方案

    来源:http://www.caoren.net/article/?id=30

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>html5之placeHolder</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    </head>
    <style>
    input{120px;height:18px;line-height:18px;border:1px #666666 solid;}
    .placeholder{position:absolute;text-indent:2px;color:#999999;font-size:12px;}
    </style>
    <body>
    <div style="padding-top:10px;">
    <input type="text" value="" id="aa" name="aa" autocomplete="off" placeholder="请输入姓名" />
    <input type="text" value="" id="bb" name="bb" />
    <input type="text" value="" id="cc" name="cc" autocomplete="off" placeholder="请输入年龄" />
    </div>
    <script type="text/javascript">
    function isPlaceHolder(){ //判断浏览器是否支持placeholder
    var input = document.createElement("input");
    return "placeholder" in input;
    }
    if(!isPlaceHolder()){
    function placeHolder(obj){
    if(!obj){return;}
    this.input = obj;
    this.label = document.createElement("label");
    this.label.innerHTML = obj.getAttribute("placeholder");
    this.label.className = "placeholder";
    if(obj.value != ""){
    this.label.style.display = "none";
    }
    this.init();
    }
    placeHolder.prototype = {
    getxy : function(obj){
    if(document.documentElement.getBoundingClientRect){
    var st=document.documentElement.scrollTop||document.body.scrollTop,
    sl=document.documentElement.scrollLeft||document.body.scrollLeft,
    ct=document.documentElement.clientTop||document.body.clientTop,
    cl=document.documentElement.clientLeft||document.body.clientLeft
    return {left:obj.getBoundingClientRect().left+sl-cl,top:obj.getBoundingClientRect().top+st-ct};
    }
    else{
    var l=t=0;
    while(obj){
    l+=obj.offsetLeft;
    t+=obj.offsetTop;
    obj=obj.offsetParent;
    }
    return {top:t,left:l}
    }
    },
    getwh : function(obj){
    return {w:obj.offsetWidth,h:obj.offsetHeight}
    },
    setStyles : function(obj,styles){
    for(var p in styles){
    obj.style[p] = styles[p]+'px';
    }
    },
    init : function(){
    var label = this.label,
    input = this.input,
    xy = this.getxy(input),
    wh = this.getwh(input);
    this.setStyles(label,{'width':wh.w,'height':wh.h,'lineHeight':wh.h,'left':xy.left,'top':xy.top});
    document.body.appendChild(this.label);
    //label.setAttribute("for",input.id);
    label.onclick = function(){
    this.style.display = "none";
    input.focus();
    }
    input.onfocus = function(){
    label.style.display = "none";
    };
    input.onblur = function(){
    if(this.value == ""){
    label.style.display = "";
    }
    };
    }
    }
    function init(){
    var inps = document.getElementsByTagName("input");
    for(var i=0,len=inps.length;i<len;i++){
    if(inps[i].getAttribute("placeholder")){
    new placeHolder(inps[i]);
    }
    }
    }
    window.onload = init;
    }
    </script>
    </body>
    </html>

  • 相关阅读:
    Python基础-面向对象1
    Centos升级安装.Net core 1.1
    员工大规模离职事件的处理方法和启示
    React Redux学习笔记
    Tfs 2015 代理池配置笔记
    自动化测试UI Test, Performance Test, Load Test 总结整理
    [转]【长文干货】浅析分布式系统
    .Net身份验证概述
    Owin中间件搭建OAuth2.0认证授权服务体会
    使用Owin中间件搭建OAuth2.0认证授权服务器
  • 原文地址:https://www.cnblogs.com/hasayaki/p/3025614.html
Copyright © 2011-2022 走看看