<!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=utf-8" /> <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" /><title></title> <style type="text/css"> .kw { color:blue; } .tips { color:#999; } /* .tips定义默认文字样式 以区别于输入文字 */ </style> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script> <script type="text/javascript"><!-- $(function(){ $("#seForm .kw").each(function(){ $(this).data( "txt", $.trim($(this).val()) ); }).focus(function(){ if ( $.trim($(this).val()) === $(this).data("txt") ) { $(this).val("").removeClass("tips"); } }).blur(function(){ if ( $.trim($(this).val()) === "" && !$(this).hasClass("once") ) { $(this).val( $(this).data("txt") ).addClass("tips"); } }); }); </script> </head> <body> <form id="seForm" method="post" action=""> <input type="text" class="kw tips" value="默认文字 always" /> <input type="text" class="kw once tips" value="默认文字 once" /> </form> </body> </html>