当前位置:编程学习 > html/css >>

兼容HTML5placeholder属性

[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
补充:web前端 , HTML 5 ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,