我想要点击输入框让默认文字清楚.然后点击外面就让默认文字还原.
这该怎么写啊?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk" />
<script type="text/javascript">
function text(){
var text = document.getElementById("text");
if(text.defaultValue.length > 0){
text.focus = function(){
text.defaultValue =0;
}
}
else{
alert("dddddd");
}
}
window.onload = text;
</script>
</head>
<body>
<h3>测试!</h3>
<input type="text" value="请输入文字!" id="text" />
</body>
</html>
这该怎么写啊?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk" />
<script type="text/javascript">
function text(){
var text = document.getElementById("text");
if(text.defaultValue.length > 0){
text.focus = function(){
text.defaultValue =0;
}
}
else{
alert("dddddd");
}
}
window.onload = text;
</script>
</head>
<body>
<h3>测试!</h3>
<input type="text" value="请输入文字!" id="text" />
</body>
</html>
如果是在IE中的话,要替换成onfocus和onblur
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk" />
<script type="text/javascript">
function text(test){
var text = document.getElementById("text");
if(text.value=="请输入文字!"){
text.value="";
}else{
if(test==""){
text.value="请输入文字!";
}else{
alert("test is a test page!");
}
}
} </script>
</head>
<body>
<h3>测试!</h3>
<input type="text" value="请输入文字!" id="text" onfocus="text(this.value)" onblur="text(this.value)"/ />
</body>
</html>
$("#mytxt").blur(function(){
if($(this).val()=="请输入..."){
$(this).attr("value","");
}
});
$("#mytxt").focus(function(){
$($(this).val()=="请输入..."){
$(this).attr("value","");
}
})
}