由於項目需求,簡單地寫了一個input默認值設置
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>input默認值設置</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script type="text/javascript">
$(function() {
//集體調用
$(".form input").each(function(){
$(this).setDefauleValue();
});
//單個調用
$("#key").setDefauleValue();
})
//設置input,textarea默認值
$.fn.setDefauleValue = function() {
var defauleValue = $(this).val();
$(this).val(defauleValue).css("color","#999");
return this.each(function() {
$(this).focus(function() {
if ($(this).val() == defauleValue) {
$(this).val("").css("color","#000");//輸入值的顏色
}
}).blur(function() {
if ($(this).val() == "") {
$(this).val(defauleValue).css("color","#999");//默認值的顏色
}
});
});
}
</script>
</head>
<body>
<form class="form">
<input type="text" size="30" value="輸入暱稱">
<br>
<input type="text" size="30" value="輸入姓名">
</form>
<br>
<br>
<br>
<input type="text" size="30" id="key" value="輸入學員ID、姓名、暱稱進行查找">
</body>
</html>