如何在html文本框中添加提示信息

如何在html文本框中添加提示信息,如账号所对应的的输入文本框内提示:请输入你的账号,然后如果我鼠标foucs到文本框内,该提示信息就会自动隐藏呢?

第1个回答  2013-04-27

用获得焦点onfocus和失去焦点onblur事件


<html>
<head>
<script>
function show(){
var test=document.getElementById("uname").value;
if(test==null || test==""){
document.getElementById("abc").style.display='';
}else{
document.getElementById("abc").style.display='none';
}
}
function hide(){
document.getElementById("abc").style.display='none';
}
</script>
</head>
<body>
<input id="uname" type="text" onblur="show()" onfocus="hide()"/><span id="abc">请输入名称</span>
</body>
</html>

第2个回答  2013-04-28
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk" />
<title>文字提示</title>
<script src="/uploads/common/js/jquery-1.4.2.min.js" type="text/javascript"></script>
<style type="text/css">
body{
margin:0;
padding:40px;
background:#fff;
font:80% Arial, Helvetica, sans-serif;
color:#555;
line-height:180%;
}
p{
clear:both;
margin:0;
padding:.5em 0;
}
/* tooltip */
#tooltip{
position:absolute;
border:1px solid #333;
background:#f7f5d1;
padding:1px;
color:#333;
display:none;
}
</style>
<script type="text/javascript">
//<![CDATA[
$(function(){
var x = 10;
var y = 20;
$("a.tooltip").mouseover(function(e){
this.myTitle = this.title;
this.title = "";
var tooltip = "<div id='tooltip'>"+ this.myTitle +"<\/div>"; //创建 div 元素
$("body").append(tooltip);//把它追加到文档中
$("#tooltip")
.css({
"top": (e.pageY+y) + "px",
"left": (e.pageX+x) + "px"
}).show("fast"); //设置x坐标和y坐标,并且显示
}).mouseout(function(){
this.title = this.myTitle;
$("#tooltip").remove(); //移除
}).mousemove(function(e){
$("#tooltip")
.css({
"top": (e.pageY+y) + "px",
"left": (e.pageX+x) + "px"
});
});
})
//]]>
</script>
</head>
<body>
<p><a href="#" class="tooltip" title="这是我的jquery超链接提示1.">提示1.</a></p>
<p><a href="#" class="tooltip" title="这是我的jquery超链接提示2.">提示2.</a></p>
<p><a href="#" title="这是自带提示1.">这是html默认的提示1.</a></p>
<p><a href="#" title="这是自带提示2.">这是html默认的提示2.</a> </p>
</body>
</html> <div style="text-align:center;margin:30px 0 0 0;"><hr style="color:#999;height:1px;">如不能显示效果,请按Ctrl+F5刷新本页,更多网页代码:<a href='http://www.veryhuo.com/' target='_blank'>http://www.veryhuo.com/</a></div>
相似回答