如题。前端页面的 input textarea 有时候须要显示默认文字以提示用户,下面为实现代码,以 input 为例。textarea 能够直接搬用
HTML
<input type="text" id="content" name="content" value="请输入内容"/>
CSS
<style type="text/css"> #content{color:#ccc; } </style>
JavaScript
<script> $("#content").focus(function(){ if(this.value == this.defaultValue) { this.value=''; $(this).css('color','#000'); } }); $("#content").blur(function(){ if(this.value == '') { this.value=this.defaultValue; $(this).css('color','#ccc'); } }); }); </script>