经典有人要这个代码,考虑到password中输入的内容会显示为*号,所以要想办法解决*的问题。当初想用setattribute动态修改type属性达到效果,再ff下测试成功,但没想到setattribute对type属性的修改在ie中无效。最终采用背景图片的方式解决了这个。即为password设定一个背景图片,图片文字为提示信息
如:<img src="http://files.jb51.net/upload/20070819124037566.gif" />,
当鼠标放上时不显示背景图片。
代码如下:
*{
margin:0px;padding:0px;font-size:12px;
}
input{
width:150px;height:20px;line-height:20px;border:1px solid #ccc;
}
function chgform(id){
if(document.getelementbyid(id).value=="密码长度必须在3-18位之间")
document.getelementbyid(id).value="";
document.getelementbyid(id).setattribute("type","password");
}
function chkform(id){
if(document.getelementbyid(id).value.length==0){
document.getelementbyid(id).setattribute("type","text");
document.getelementbyid(id).value="密码长度必须在3-18位之间";
}
}
function chkformpic(id){
if(document.getelementbyid(id).value.length==0){
document.getelementbyid(id).style.background='url(/upload/20070819124037566.gif) no-repeat';
}
}
姓名:
动态修改input的type属性(仅在ff下有效):
密码:
图片提示(在ff/ie下均有效):
密码:
[ctrl+a 全选 注:如需引入外部js需刷新才能执行]