赞
踩
文本比较实现思路:
while (index1 < texts1.length || index2 < texts2.length) {}
if (texts1[index1] === texts2[index2]) {
实现代码:
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>文本比较</title>
- <style>
- * {
- margin: 0;
- padding: 0;
- box-sizing: border-box;
- }
-
- .text_content {
- height: 24px;
- font-size: 14px;
- line-height: 24px;
- box-sizing: border-box;
- }
-
- .serial {
- display: flex;
- width: 50px;
- }
-
- .serial .serialNum {
- float: right;
- width: 40px;
- }
-
- .serialNum {
- font-size: 14px;
- text-align: right;
- padding-right: 5px;
- height: 24px;
- line-height: 24px;
- }
-
- .serialLogo {
- font-size: 16px;
- text-align: right;
- height: 24px;
- color: #0000A0;
- font-weight: bold;
- width: 10px;
- }
-
- #details2 .change {
- background-color: #FFFF00;
- }
-
- #details1 .change {
- background-color: #FDD017;
- }
-
- .empty {
- background-color: #C0C0C0;
- }
-
- </style>
- </head>
- <body style="position: absolute;width: 100%;height: 100%">
- <div style="position: absolute;width: 100%;height: 100%">
- <div style="position: absolute;top:10px;bottom:20px;left:0;width: 100%;">
- <div id="data1"
- style="position: absolute;top:0;height: 100%;left: 0;width: 50%;border: 2px silver solid;overflow: scroll">
- <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">
- <span style="font-size: 14px;color: black;">原代码</span>
- <button onclick="copy(1)"
- style="font-size: 14px;color: black;float: right; padding:0 5px;margin: 0 10px">复制代码
- </button>
- </div>
- <div style="min-height: calc(100% - 30px);margin-top: 30px">
- <div id="serial1" class="serialDiv"
- style="position: absolute;left: 0;width: 50px;min-height:calc(100% - 30px);background-color: #B6B6B4">
-
- </div>
- <div id="details1" style="position: absolute;left: 50px;right: 0;min-height:calc(100% - 30px);">
-
- </div>
- </div>
- </div>
- <div id="data2"
- style="position: absolute;top:0;height: 100%;left: 50%;width:50% ;border: 2px silver solid;overflow: scroll">
- <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">
- <span style="font-size: 14px;color: black;">更新代码</span>
- <button onclick="copy(2)"
- style="font-size: 14px;color: black;float: right; padding:0 5px;margin: 0 10px">复制代码
- </button>
- </div>
- <div style="min-height: calc(100% - 30px);margin-top: 30px">
- <div id="serial2" class="serialDiv"
- style="position: absolute;left: 0;width: 50px;min-height:calc(100% - 30px);background-color: #B6B6B4">
-
- </div>
- <div id="details2" style="position: absolute;left: 50px;right: 0;min-height:calc(100% - 30px);">
-
- </div>
- </div>
- </div>
- </div>
- </div>
- <script src="jquery-3.6.0.min.js"></script>
- <!--<script src="text1.js"></script>-->
- <!--<script src="text2.js"></script>-->
- <script>
- let text1="文本1\n文本比较";
- let text2="文本2\n文本比较";
- $(function () {
- let texts1 = text1.split("\n");
- let texts2 = text2.split("\n");
- let details;
- comparison(texts1, texts2);
-
-
- //设置滚动条同步滚动
- $("#data1").mouseover(function () {
- $(this).on("scroll", function () {
- console.log(1)
- $("#data2").scrollTop($(this).scrollTop()); // 纵向滚动条
- $("#data2").scrollLeft($(this).scrollLeft()); // 横向滚动条
- });
- details = document.getElementById("details1");
- }).mouseout(function () {
- $(this).unbind("scroll");
- });
- $("#data2").mouseover(function () {
- $(this).on("scroll", function () {
- $("#data1").scrollTop($(this).scrollTop()).scrollLeft($(this).scrollLeft());
- });
- details = document.getElementById("details2");
- }).mouseout(function () {
- $(this).unbind("scroll");
- });
-
-
- //设置文本全选
- $("body").keydown(function (event) {
- if (event.ctrlKey && event.which === 65) {
- let selection = window.getSelection();
- let range = document.createRange();
- range.selectNodeContents(details);
- selection.removeAllRanges();
- selection.addRange(range);
- return false;
- }
- });
-
- $(".serialNum").on("click", function () {
- let id = $(this).attr("id");
- let index = id.split("_")[1];
- setBorder(index);
- });
- $(".text_content").on("click", function () {
- let id = $(this).attr("id");
- let index = id.split("_")[1];
- setBorder(index);
- });
- });
-
-
- function setBorder(index) {
- $(".text_content").css("border", "0");
- $(`#text1_${index}`).css({"border-top": "1px black solid", "border-bottom": "1px black solid"});
- $(`#text2_${index}`).css({"border-top": "1px black solid", "border-bottom": "1px black solid"});
- }
-
- //复制
- function copy(type) {
- let text;
- if (type === 1) {
- text = text1;
- } else {
- text = text2;
- }
- let oInput = document.createElement('textarea');
- oInput.value = text;
- document.body.appendChild(oInput);
- oInput.select(); // 选择对象
- document.execCommand("Copy"); // 执行浏览器复制命令
- oInput.className = 'oInput';
- oInput.style.display = 'none';
- alert('复制成功');
- }
-
- //对比
- function comparison(texts1, texts2) {
- let newTexts1 = [];
- let newTexts2 = [];
- let index = 0;
- let index1 = 0;
- let index2 = 0;
- let serial1 = $("#serial1");
- let serial2 = $("#serial2");
- while (index1 < texts1.length || index2 < texts2.length) {
- if (index1 >= texts1.length) {
- newTexts1.push(`<xmp id="text1_${index}" class="text_content empty"></xmp>`);
- newTexts2.push(`<xmp id="text2_${index}" class="text_content change">${texts2[index2]}</xmp>`);
- serial1.append(`<div id="serial1_${index}" class="serialNum"></div>`);
- serial2.append(`<div id="serial2_${index}" class="serialNum">${index2 + 1}</div>`);
- index2++;
- index++;
- continue;
- }
- if (index2 >= texts2.length) {
- newTexts1.push(`<xmp id="text1_${index}" class="text_content change">${texts1[index1]}</xmp>`);
- newTexts2.push(`<xmp id="text2_${index}" class="text_content empty"></xmp>`);
- serial1.append(`<div id="serial1_${index}" class="serialNum">${index1 + 1}</div>`);
- serial2.append(`<div id="serial2_${index}" class="serialNum"></div>`);
- index++;
- index1++;
- index2++;
- continue;
- }
- //相同
- if (texts1[index1] === texts2[index2]) {
- newTexts1.push(`<xmp id="text1_${index}" class="text_content equal">${texts1[index1]}</xmp>`);
- newTexts2.push(`<xmp id="text2_${index}" class="text_content equal">${texts2[index2]}</xmp>`);
- serial1.append(`<div id="serial1_${index}" class="serialNum">${index1 + 1}</div>`);
- serial2.append(`<div id="serial2_${index}" class="serialNum">${index2 + 1}</div>`);
- index++;
- index1++;
- index2++;
- } else {
- // 判断texts2有没有
- let int2 = index2 + 1;
- let has = false;
- while (int2 < texts2.length) {
- if (texts1[index1] === texts2[int2]) {
- has = true;
- break;
- } else {
- int2++;
- }
-
- }
- if (has) {
- while (index2 < int2) {
- newTexts1.push(`<xmp id="text1_${index}" class="text_content empty"></xmp>`);
- newTexts2.push(`<xmp id="text2_${index}" class="text_content change">${texts2[index2]}</xmp>`);
- serial1.append(`<div id="serial1_${index}" class="serialNum"></div>`);
- serial2.append(`<div class="serial"><div class="serialLogo">+</div><div id="serial2_${index}" class="serialNum">${index2 + 1}</div></div>`);
- index2++;
- index++;
- }
- newTexts1.push(`<xmp id="text1_${index}" class="text_content equal">${texts1[index1]}</xmp>`);
- newTexts2.push(`<xmp id="text2_${index}" class="text_content equal">${texts2[index2]}</xmp>`);
- serial1.append(`<div id="serial1_${index}" class="serialNum">${index1 + 1}</div>`);
- serial2.append(`<div id="serial2_${index}" class="serialNum">${index2 + 1}</div>`);
- index1++;
- index2++;
- index++;
- } else {
- // 判断texts2有没有
- let int1 = index1 + 1;
- has = false;
- while (int1 < texts1.length) {
- if (texts1[int1] === texts2[index2]) {
- has = true;
- break;
- } else {
- int1++;
- }
-
- }
- if (has) {
- while (index1 < int1) {
- newTexts1.push(`<xmp id="text1_${index}" class="text_content change">${texts1[index1]}</xmp>`);
- newTexts2.push(`<xmp id="text2_${index}" class="text_content empty"></xmp>`);
- serial1.append(`<div class="serial"><div class="serialLogo">-</div><div id="serial1_${index}" class="serialNum">${index1 + 1}</div></div>`);
- serial2.append(`<div id="serial2_${index}" class="serialNum"></div>`);
- index1++;
- index++;
- }
- newTexts1.push(`<xmp id="text1_${index}" class="text_content equal">${texts1[index1]}</xmp>`);
- newTexts2.push(`<xmp id="text2_${index}" class="text_content equal">${texts2[index2]}</xmp>`);
- serial1.append(`<div id="serial1_${index}" class="serialNum">${index1 + 1}</div>`);
- serial2.append(`<div id="serial2_${index}" class="serialNum">${index2 + 1}</div>`);
- index1++;
- index2++;
- index++;
- } else {
- newTexts1.push(`<xmp id="text1_${index}" class="text_content change">${texts1[index1]}</xmp>`);
- newTexts2.push(`<xmp id="text2_${index}" class="text_content change">${texts2[index2]}</xmp>`);
- serial1.append(`<div class="serial"><div class="serialLogo">-</div><div id="serial1_${index}" class="serialNum">${index1 + 1}</div></div>`);
- serial2.append(`<div class="serial"><div class="serialLogo">+</div><div id="serial2_${index}" class="serialNum">${index2 + 1}</div></div>`);
- index1++;
- index2++;
- index++;
- }
-
- }
- }
- }
-
-
- $("#details1").append(newTexts1);
- $("#details2").append(newTexts2);
- }
-
-
- </script>
- </body>
- </html>
备注:只是简单的行比较,可能比较不是很全。可以继续优化,例如:不同行显示修改的是哪个文字
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。