<input type="text" id="uname" name="uname" placeholder="请输入用户名"/>CSS:
.phcolor{ color:#999;}
JS:
$(function(){
//判断浏览器是否支持placeholder属性
supportPlaceholder='placeholder'in document.createElement('input'),
placeholder=function(input){
var text = input.attr('placeholder'),
defaultValue = input.defaultValue;
if(!defaultValue){
input.val(text).addClass("phcolor");
}
input.focus(function(){
if(input.val() == text){
$(this).val("");
}
});
input.blur(function(){
if(input.val() == ""){
$(this).val(text).addClass("phcolor");
}
});
//输入的字符不为灰色
input.keydown(function(){
$(this).removeClass("phcolor");
});
};
//当浏览器不支持placeholder属性时,调用placeholder函数
if(!supportPlaceholder){
$('input').each(function(){
text = $(this).attr("placeholder");
if($(this).attr("type") == "text"){
placeholder($(this));
}
});
}
});
.phbg{ background:url(img/bg.jpg) 0 0 no-repeat;}
JS:
$(function(){
//判断浏览器是否支持placeholder属性
supportPlaceholder='placeholder' in document.createElement('input');
if(!supportPlaceholder){
//初始状态添加背景图片
$("#uname").attr("class","phbg");
//初始状态获得焦点
$("#uname").focus;
function destyle(){
if($("#uname").val() != ""){
$("#uname").removeClass("phbg");
}else{
$("#uname").attr("class","phbg");
}
}
//当输入框为空时,添加背景图片;有值时去掉背景图片
destyle();
$("#uname").keyup(function(){
//当输入框有按键输入同时输入框不为空时,去掉背景图片;有按键输入同时为空时(删除字符),添加背景图片
destyle();
});
$("#uname").keydown(function(){
//keydown事件可以在按键按下的第一时间去掉背景图片
$("#uname").removeClass("phbg");
});
}
});
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有