Js实现鼠标点击时自动选中文本框文本,相信许多朋友都遇到这种功能的,就是当我们点击文本框的时候,文本框中默认的文字会被全部选中,这样你不用再去刻意选择了,省去了一些麻烦,提高了输入效率,用户肯定会喜欢这功能,那么实现的方法是怎么样呢?通过本代码你就搞明白了。 效果如图:

全部代码如下:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> <title>自动选择文本框/编辑框中的文字</title> <script type="text/javascript"> function Myselect_txt(){ if (document.form1.title.focus){ document.form1.title.select();} } function Myselect_txtarea(){ if (document.form1.content.focus){ document.form1.content.select();} } window.onload = function(){ var title = document.getElementById("title"); var content = document.getElementById("content"); title.onclick = function(){ Myselect_txt(); } content.onclick = function(){ Myselect_txtarea(); } } </script> </head> <body style="font-size:12px"> <table width="443" height="97" border="0" align="center" cellpadding="0" cellspacing="0" bgcolor="#f9f9f9"> <form name="form1"> <tr bgcolor="#214994"> <td height="25" colspan="2"><div align="center"><font color="#FFFFFF">新闻信息修改</font></div></td> </tr> <tr> <td width="80" height="28"><div align="right"><font color="#214994">新闻标题:</font></div></td> <td width="363"><input id="title" name="title" type="text" size="50" value="今日新闻头条"></td> </tr> <tr> <td height="20"><div align="right"><font color="#214994">新闻内容:</font></div></td> <td rowspan="2"><textarea id="content" name="content" cols="50" rows="6">今日,据相关方面报道,...</textarea></td> </tr> <tr> <td height="10"> </td> </tr> <tr> <td height="32" colspan="2"> <div align="center"> <input name="add" type="submit" id="add" value="添加"> <input name="Submit" type="reset" value="重置"> </div></td> </tr> </form> </table> </body> </html>
JS代码:
<script type="text/javascript"> function Myselect_txt(){ if (document.form1.title.focus){ document.form1.title.select();} } function Myselect_txtarea(){ if (document.form1.content.focus){ document.form1.content.select();} } window.onload = function(){ var title = document.getElementById("title"); var content = document.getElementById("content"); title.onclick = function(){ Myselect_txt(); } content.onclick = function(){ Myselect_txtarea(); } } </script>