findreplace.htm 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <HTML>
  2. <HEAD>
  3. <TITLE>查找 / 替换</TITLE>
  4. <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
  5. <style type="text/css">
  6. body, a, table, div, span, td, th, input, select{font-size:9pt;font-family: "宋体", Verdana, Arial, Helvetica, sans-serif;}
  7. body {padding:5px}
  8. </style>
  9. <script language="JavaScript">
  10. // 初始选定对象
  11. var oSelection;
  12. oSelection = dialogArguments.document.selection.createRange();
  13. // 返回匹配条件
  14. function searchtype(){
  15. var retval = 0;
  16. var matchcase = 0;
  17. var matchword = 0;
  18. if (document.frmSearch.blnMatchCase.checked) matchcase = 4;
  19. if (document.frmSearch.blnMatchWord.checked) matchword = 2;
  20. retval = matchcase + matchword;
  21. return(retval);
  22. }
  23. // 检测输入
  24. function checkInput(){
  25. if (document.frmSearch.strSearch.value.length < 1) {
  26. alert("请输入查找内容");
  27. return false;
  28. } else {
  29. return true;
  30. }
  31. }
  32. // 查找
  33. function findtext(){
  34. if (checkInput()) {
  35. var searchval = document.frmSearch.strSearch.value;
  36. oSelection.collapse(false);
  37. if (oSelection.findText(searchval, 1000000000, searchtype())) {
  38. oSelection.select();
  39. } else {
  40. var startfromtop = confirm("搜索完成,是否要从顶部开始重新搜索?");
  41. if (startfromtop) {
  42. oSelection.expand("textedit");
  43. oSelection.collapse();
  44. oSelection.select();
  45. findtext();
  46. }
  47. }
  48. }
  49. }
  50. // 在选中的文本中替换
  51. function replacetext(){
  52. if (checkInput()) {
  53. if (document.frmSearch.blnMatchCase.checked){
  54. if (oSelection.text == document.frmSearch.strSearch.value) oSelection.text = document.frmSearch.strReplace.value
  55. } else {
  56. if (oSelection.text.toLowerCase() == document.frmSearch.strSearch.value.toLowerCase()) oSelection.text = document.frmSearch.strReplace.value
  57. }
  58. findtext();
  59. }
  60. }
  61. // 在所有内容中替换
  62. function replacealltext(){
  63. if (checkInput()) {
  64. var searchval = document.frmSearch.strSearch.value;
  65. var wordcount = 0;
  66. var msg = "";
  67. oSelection.expand("textedit");
  68. oSelection.collapse();
  69. oSelection.select();
  70. while (oSelection.findText(searchval, 1000000000, searchtype())){
  71. oSelection.select();
  72. oSelection.text = document.frmSearch.strReplace.value;
  73. wordcount++;
  74. }
  75. if (wordcount == 0) msg = "要查找的内容没有找到"
  76. else msg = wordcount + " 处文本被替换成功";
  77. alert(msg);
  78. }
  79. }
  80. </script>
  81. </HEAD>
  82. <BODY bgcolor="menu">
  83. <FORM NAME="frmSearch" method="post" action="">
  84. <TABLE CELLSPACING="0" cellpadding="5" border="0">
  85. <TR><TD VALIGN="top" align="left" nowrap>
  86. <label for="strSearch">查找内容</label><br>
  87. <INPUT TYPE=TEXT SIZE=40 NAME=strSearch id="strSearch" style="width : 200px;"><br>
  88. <label for="strReplace">替换内容</label><br>
  89. <INPUT TYPE=TEXT SIZE=40 NAME=strReplace id="strReplace" style="width : 200px;"><br>
  90. <INPUT TYPE=Checkbox SIZE=40 NAME=blnMatchCase ID="blnMatchCase"><label for="blnMatchCase">区分大小写</label><br>
  91. <INPUT TYPE=Checkbox SIZE=40 NAME=blnMatchWord ID="blnMatchWord"><label for="blnMatchWord">全部匹配</label>
  92. </td>
  93. <td rowspan="2" valign="top">
  94. <input type=button style="width:80px;margin-top:15px" name="btnFind" onClick="findtext();" value="查找下一个"><br>
  95. <input type=button style="width:80px;margin-top:5px" name="btnCancel" onClick="window.close();" value="关闭"><br>
  96. <input type=button style="width:80px;margin-top:5px" name="btnReplace" onClick="replacetext();" value="替换"><br>
  97. <input type=button style="width:80px;margin-top:5px" name="btnReplaceall" onClick="replacealltext();" value="全部替换"><br>
  98. </td>
  99. </tr>
  100. </table>
  101. </FORM>
  102. </BODY>
  103. </HTML>