欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

兼容HTML5placeholder属性

程序员文章站 2022-07-05 23:07:12
[javascript]  var placeholder=function(arrobj){      if(!('placeholder...

[javascript] 
var placeholder=function(arrobj){ 
    if(!('placeholder' in document.createelement('input'))) 
    { 
        var bind=function(name,val){ 
            name.focus(function(){ 
                var o=$(this); 
                if(o.val()===val) 
                { 
                    o.val("");      
                    o.css({"color":"#000"}) 
                } 
            }).blur(function(){ 
                var o=$(this); 
                if(!o.val()||o.val()===val) 
                { 
                    o.val(val);    
                    o.css({"color":"#c0c0c0"}) 
                }  
            }).blur(); 
        }; 
        if(arrobj){ 
            for(var i in arrobj) 
            { 
                var txt=$(arrobj[i]["name"]); 
                var textdefault=arrobj[i]["val"]  ||"请输入";                                  
                bind(txt,textdefault); 
            }    
        } 
        else 
        { 
            $("input[placeholder]").each(function(){ 
                var o=$(this) 
                bind(o,o.attr("placeholder"));                    
            }) 
        } 
    }else 
    { 
        for(var i in arrobj) 
        { 
            var txt=$(arrobj[i]["name"]); 
            var textdefault=arrobj[i]["val"]  ||"请输入";                                  
            txt.attr("placeholder",textdefault) 
        }    
    } 


[javascript]
//使用方式1 
                placeholder(); 
                //使用方式2 
                /*placeholder([
                    { name:"#name",val:"请输入用户名/邮箱"},
                    {name:"#pwd",val:"请输入你的密码"}
                ])*/ 


作者:chaiyining007