jQuery实现input文本框内灰色提示文本效果 和 input标签获取焦点是文本框内提示信息清空...

一、jQuery实现input文本框内灰色提示文本效果

 

<html>  
<head>  
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
<title>input test</title>  
<script type="text/javascript"
    src="./js/jquery.js"></script>
<script type="text/javascript">  
function inputTipText(){    
$("input[class*=grayTips]") //所有样式名中含有grayTips的input   
.each(function(){   
   var oldVal=$(this).val();   //默认的提示性文本   
   $(this)   
   .css({"color":"#888"})  //灰色   
   .focus(function(){   
    if($(this).val()!=oldVal){$(this).css({"color":"#000"})}else{$(this).val("").css({"color":"#888"})}   
   })   
   .blur(function(){   
    if($(this).val()==""){$(this).val(oldVal).css({"color":"#888"})}   
   })   
   .keydown(function(){$(this).css({"color":"#000"})})   
     
})   
}   
  
$(function(){   
inputTipText();  //直接调用就OK了   
})   
</script>  
</head>  
<body>  
<input type="text" value="输入您的用户名" class="grayTips" />  
<input type="text" value="输入您的登录密码" class="aaagrayTips"/>  
</body>  
</html>
二、input标签获取焦点是文本框内提示信息清空 
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<script language="javascript" type="text/javascript"> 
function addLoadEvent(func){
    var oldonload = window.onload;
    if (typeof window.onload != 'function'){
        window.onload = func;
    }else{
        window.onload = function(){
            oldonload();
            func();
        }
    }
}
function checkText(){
    var textId = document.getElementById('test');
        //设置文本框中的字体颜色为灰色
        document.getElementById('test').style.color='#C0C0C0';
    var textDefault = '中文';
    function cls(){
        if (this.value == textDefault){
            this.value = '';
        }
    }
    function res(){
        if (this.value == ''){
            this.value = textDefault;
        }
    }
    textId.onfocus = cls;
    textId.onblur = res;
}
addLoadEvent (checkText);
</script>
<body>
<input type="text" id="test" value="中文" />
</body>
</html>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值