当前位置:   article > 正文

JS 文本比较_js 文本对比

js 文本对比

 文本比较实现思路:

  1.  把文本内容根据换行符进行分割,转换成texts1、tests2数组
  2.  进行循环对比,当两个数组都循环完毕结束
    while (index1 < texts1.length || index2 < texts2.length) {}
  3.  进行是否相同判断
    if (texts1[index1] === texts2[index2]) {
  4. 不相同时,其中一个数组下标循环+1进行比较

 

 实现代码:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>文本比较</title>
  6. <style>
  7. * {
  8. margin: 0;
  9. padding: 0;
  10. box-sizing: border-box;
  11. }
  12. .text_content {
  13. height: 24px;
  14. font-size: 14px;
  15. line-height: 24px;
  16. box-sizing: border-box;
  17. }
  18. .serial {
  19. display: flex;
  20. width: 50px;
  21. }
  22. .serial .serialNum {
  23. float: right;
  24. width: 40px;
  25. }
  26. .serialNum {
  27. font-size: 14px;
  28. text-align: right;
  29. padding-right: 5px;
  30. height: 24px;
  31. line-height: 24px;
  32. }
  33. .serialLogo {
  34. font-size: 16px;
  35. text-align: right;
  36. height: 24px;
  37. color: #0000A0;
  38. font-weight: bold;
  39. width: 10px;
  40. }
  41. #details2 .change {
  42. background-color: #FFFF00;
  43. }
  44. #details1 .change {
  45. background-color: #FDD017;
  46. }
  47. .empty {
  48. background-color: #C0C0C0;
  49. }
  50. </style>
  51. </head>
  52. <body style="position: absolute;width: 100%;height: 100%">
  53. <div style="position: absolute;width: 100%;height: 100%">
  54. <div style="position: absolute;top:10px;bottom:20px;left:0;width: 100%;">
  55. <div id="data1"
  56. style="position: absolute;top:0;height: 100%;left: 0;width: 50%;border: 2px silver solid;overflow: scroll">
  57. <div style="position:fixed;height: 30px;width: calc(50% - 20px);background-color: #FDD017;border: 2px #ADD8E6 solid;text-align: center;z-index: 1000;line-height: 26px">
  58. <span style="font-size: 14px;color: black;">原代码</span>
  59. <button onclick="copy(1)"
  60. style="font-size: 14px;color: black;float: right; padding:0 5px;margin: 0 10px">复制代码
  61. </button>
  62. </div>
  63. <div style="min-height: calc(100% - 30px);margin-top: 30px">
  64. <div id="serial1" class="serialDiv"
  65. style="position: absolute;left: 0;width: 50px;min-height:calc(100% - 30px);background-color: #B6B6B4">
  66. </div>
  67. <div id="details1" style="position: absolute;left: 50px;right: 0;min-height:calc(100% - 30px);">
  68. </div>
  69. </div>
  70. </div>
  71. <div id="data2"
  72. style="position: absolute;top:0;height: 100%;left: 50%;width:50% ;border: 2px silver solid;overflow: scroll">
  73. <div style="position:fixed;height: 30px;width: calc(50% - 20px);background-color: #FFFF00;border: 2px #ADD8E6 solid;text-align: center;z-index: 1000;line-height: 26px">
  74. <span style="font-size: 14px;color: black;">更新代码</span>
  75. <button onclick="copy(2)"
  76. style="font-size: 14px;color: black;float: right; padding:0 5px;margin: 0 10px">复制代码
  77. </button>
  78. </div>
  79. <div style="min-height: calc(100% - 30px);margin-top: 30px">
  80. <div id="serial2" class="serialDiv"
  81. style="position: absolute;left: 0;width: 50px;min-height:calc(100% - 30px);background-color: #B6B6B4">
  82. </div>
  83. <div id="details2" style="position: absolute;left: 50px;right: 0;min-height:calc(100% - 30px);">
  84. </div>
  85. </div>
  86. </div>
  87. </div>
  88. </div>
  89. <script src="jquery-3.6.0.min.js"></script>
  90. <!--<script src="text1.js"></script>-->
  91. <!--<script src="text2.js"></script>-->
  92. <script>
  93. let text1="文本1\n文本比较";
  94. let text2="文本2\n文本比较";
  95. $(function () {
  96. let texts1 = text1.split("\n");
  97. let texts2 = text2.split("\n");
  98. let details;
  99. comparison(texts1, texts2);
  100. //设置滚动条同步滚动
  101. $("#data1").mouseover(function () {
  102. $(this).on("scroll", function () {
  103. console.log(1)
  104. $("#data2").scrollTop($(this).scrollTop()); // 纵向滚动条
  105. $("#data2").scrollLeft($(this).scrollLeft()); // 横向滚动条
  106. });
  107. details = document.getElementById("details1");
  108. }).mouseout(function () {
  109. $(this).unbind("scroll");
  110. });
  111. $("#data2").mouseover(function () {
  112. $(this).on("scroll", function () {
  113. $("#data1").scrollTop($(this).scrollTop()).scrollLeft($(this).scrollLeft());
  114. });
  115. details = document.getElementById("details2");
  116. }).mouseout(function () {
  117. $(this).unbind("scroll");
  118. });
  119. //设置文本全选
  120. $("body").keydown(function (event) {
  121. if (event.ctrlKey && event.which === 65) {
  122. let selection = window.getSelection();
  123. let range = document.createRange();
  124. range.selectNodeContents(details);
  125. selection.removeAllRanges();
  126. selection.addRange(range);
  127. return false;
  128. }
  129. });
  130. $(".serialNum").on("click", function () {
  131. let id = $(this).attr("id");
  132. let index = id.split("_")[1];
  133. setBorder(index);
  134. });
  135. $(".text_content").on("click", function () {
  136. let id = $(this).attr("id");
  137. let index = id.split("_")[1];
  138. setBorder(index);
  139. });
  140. });
  141. function setBorder(index) {
  142. $(".text_content").css("border", "0");
  143. $(`#text1_${index}`).css({"border-top": "1px black solid", "border-bottom": "1px black solid"});
  144. $(`#text2_${index}`).css({"border-top": "1px black solid", "border-bottom": "1px black solid"});
  145. }
  146. //复制
  147. function copy(type) {
  148. let text;
  149. if (type === 1) {
  150. text = text1;
  151. } else {
  152. text = text2;
  153. }
  154. let oInput = document.createElement('textarea');
  155. oInput.value = text;
  156. document.body.appendChild(oInput);
  157. oInput.select(); // 选择对象
  158. document.execCommand("Copy"); // 执行浏览器复制命令
  159. oInput.className = 'oInput';
  160. oInput.style.display = 'none';
  161. alert('复制成功');
  162. }
  163. //对比
  164. function comparison(texts1, texts2) {
  165. let newTexts1 = [];
  166. let newTexts2 = [];
  167. let index = 0;
  168. let index1 = 0;
  169. let index2 = 0;
  170. let serial1 = $("#serial1");
  171. let serial2 = $("#serial2");
  172. while (index1 < texts1.length || index2 < texts2.length) {
  173. if (index1 >= texts1.length) {
  174. newTexts1.push(`<xmp id="text1_${index}" class="text_content empty"></xmp>`);
  175. newTexts2.push(`<xmp id="text2_${index}" class="text_content change">${texts2[index2]}</xmp>`);
  176. serial1.append(`<div id="serial1_${index}" class="serialNum"></div>`);
  177. serial2.append(`<div id="serial2_${index}" class="serialNum">${index2 + 1}</div>`);
  178. index2++;
  179. index++;
  180. continue;
  181. }
  182. if (index2 >= texts2.length) {
  183. newTexts1.push(`<xmp id="text1_${index}" class="text_content change">${texts1[index1]}</xmp>`);
  184. newTexts2.push(`<xmp id="text2_${index}" class="text_content empty"></xmp>`);
  185. serial1.append(`<div id="serial1_${index}" class="serialNum">${index1 + 1}</div>`);
  186. serial2.append(`<div id="serial2_${index}" class="serialNum"></div>`);
  187. index++;
  188. index1++;
  189. index2++;
  190. continue;
  191. }
  192. //相同
  193. if (texts1[index1] === texts2[index2]) {
  194. newTexts1.push(`<xmp id="text1_${index}" class="text_content equal">${texts1[index1]}</xmp>`);
  195. newTexts2.push(`<xmp id="text2_${index}" class="text_content equal">${texts2[index2]}</xmp>`);
  196. serial1.append(`<div id="serial1_${index}" class="serialNum">${index1 + 1}</div>`);
  197. serial2.append(`<div id="serial2_${index}" class="serialNum">${index2 + 1}</div>`);
  198. index++;
  199. index1++;
  200. index2++;
  201. } else {
  202. // 判断texts2有没有
  203. let int2 = index2 + 1;
  204. let has = false;
  205. while (int2 < texts2.length) {
  206. if (texts1[index1] === texts2[int2]) {
  207. has = true;
  208. break;
  209. } else {
  210. int2++;
  211. }
  212. }
  213. if (has) {
  214. while (index2 < int2) {
  215. newTexts1.push(`<xmp id="text1_${index}" class="text_content empty"></xmp>`);
  216. newTexts2.push(`<xmp id="text2_${index}" class="text_content change">${texts2[index2]}</xmp>`);
  217. serial1.append(`<div id="serial1_${index}" class="serialNum"></div>`);
  218. serial2.append(`<div class="serial"><div class="serialLogo">+</div><div id="serial2_${index}" class="serialNum">${index2 + 1}</div></div>`);
  219. index2++;
  220. index++;
  221. }
  222. newTexts1.push(`<xmp id="text1_${index}" class="text_content equal">${texts1[index1]}</xmp>`);
  223. newTexts2.push(`<xmp id="text2_${index}" class="text_content equal">${texts2[index2]}</xmp>`);
  224. serial1.append(`<div id="serial1_${index}" class="serialNum">${index1 + 1}</div>`);
  225. serial2.append(`<div id="serial2_${index}" class="serialNum">${index2 + 1}</div>`);
  226. index1++;
  227. index2++;
  228. index++;
  229. } else {
  230. // 判断texts2有没有
  231. let int1 = index1 + 1;
  232. has = false;
  233. while (int1 < texts1.length) {
  234. if (texts1[int1] === texts2[index2]) {
  235. has = true;
  236. break;
  237. } else {
  238. int1++;
  239. }
  240. }
  241. if (has) {
  242. while (index1 < int1) {
  243. newTexts1.push(`<xmp id="text1_${index}" class="text_content change">${texts1[index1]}</xmp>`);
  244. newTexts2.push(`<xmp id="text2_${index}" class="text_content empty"></xmp>`);
  245. serial1.append(`<div class="serial"><div class="serialLogo">-</div><div id="serial1_${index}" class="serialNum">${index1 + 1}</div></div>`);
  246. serial2.append(`<div id="serial2_${index}" class="serialNum"></div>`);
  247. index1++;
  248. index++;
  249. }
  250. newTexts1.push(`<xmp id="text1_${index}" class="text_content equal">${texts1[index1]}</xmp>`);
  251. newTexts2.push(`<xmp id="text2_${index}" class="text_content equal">${texts2[index2]}</xmp>`);
  252. serial1.append(`<div id="serial1_${index}" class="serialNum">${index1 + 1}</div>`);
  253. serial2.append(`<div id="serial2_${index}" class="serialNum">${index2 + 1}</div>`);
  254. index1++;
  255. index2++;
  256. index++;
  257. } else {
  258. newTexts1.push(`<xmp id="text1_${index}" class="text_content change">${texts1[index1]}</xmp>`);
  259. newTexts2.push(`<xmp id="text2_${index}" class="text_content change">${texts2[index2]}</xmp>`);
  260. serial1.append(`<div class="serial"><div class="serialLogo">-</div><div id="serial1_${index}" class="serialNum">${index1 + 1}</div></div>`);
  261. serial2.append(`<div class="serial"><div class="serialLogo">+</div><div id="serial2_${index}" class="serialNum">${index2 + 1}</div></div>`);
  262. index1++;
  263. index2++;
  264. index++;
  265. }
  266. }
  267. }
  268. }
  269. $("#details1").append(newTexts1);
  270. $("#details2").append(newTexts2);
  271. }
  272. </script>
  273. </body>
  274. </html>

 备注:只是简单的行比较,可能比较不是很全。可以继续优化,例如:不同行显示修改的是哪个文字

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/神奇cpp/article/detail/796931
推荐阅读