IT学习联盟

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
IT学习联盟 门户 文章 HTML 5 查看内容

兼容HTML5placeholder属性

2012-8-18 17:12| 发布者: admin| 查看: 533| 评论: 0

[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:"请输入你的密码"}
                ])*/ 


Archiver|IT学习联盟| 网站地图

Powered by Discuz! X2.5© 2001-2010 世界学习室 大伟制作

GMT+8, 2018-5-26 00:51 , Processed in 0.106537 second(s), 12 queries .