纯JS文本比较工具

前段时间由于工作需要写了一个纯JS文本比较工具

在这里与大家分享下

算法有待优化,还希望大家多多指教

先上效果图:

 

 

奉上源码(把源码保存为html格式的文件就可以直接运行了):


<!doctype html>
<html>
    <head>
        <title>文本比较工具</title>
        <style type="text/css">
            *{padding:0px;margin:0px;}
            html,body{
                overflow-y: hidden;
            }
            .edit_div{
                border: 1px solid #CCCCCC;
                overflow: auto;
                position: relative;
            }
            .edit_div textarea{
                resize:none;
                background: none repeat scroll 0 0 transparent;
                border: 0 none;
                width: 100%;
                height:200px;
                overflow-y: scroll;
                position: absolute;
                left: 0px;
                top: 0px;
                z-index: 2;
                font-size: 18px;
                white-space: pre-wrap;
                word-wrap: break-word;
                word-break:break-all;
            }
            .edit_div pre{
                overflow-y: scroll;
                white-space: pre-wrap;
                word-wrap: break-word;
                word-break:break-all;
                width: 100%;
                height:200px;
                text-align: left;
                color: #ffffff;
                z-index: 1;
                font-size: 18px;
            }
    </style>
    </head>
    <body>
        <table style="width:100%">
            <tr>
                <td style="width:50%">
                    <div class="edit_div">
                        <pre id="edit_pre_1"></pre>
                        <textarea id="edit_textarea_1" onscroll="test1_scroll()" oninput="textchange()" onpropertychange="textchange()"></textarea>
                    </div>
                </td>
                <td style="width:50%">
                    <div class="edit_div">
                        <pre  id="edit_pre_2"></pre>
                        <textarea id="edit_textarea_2" onscroll="test2_scroll()" oninput="textchange()" onpropertychange="textchange()"></textarea>
                    </div>
                </td>
            </tr>
        </table>
        <script type="text/javascript">
            function test1_scroll(){
                document.getElementById("edit_pre_1").scrollTop=document.getElementById("edit_textarea_1").scrollTop;
                document.getElementById("edit_pre_2").scrollTop=document.getElementById("edit_pre_1").scrollTop;
                document.getElementById("edit_textarea_2").scrollTop=document.getElementById("edit_textarea_1").scrollTop;
            }
            function test2_scroll(){
                document.getElementById("edit_pre_2").scrollTop=document.getElementById("edit_textarea_2").scrollTop;
                document.getElementById("edit_pre_1").scrollTop=document.getElementById("edit_pre_2").scrollTop;
                document.getElementById("edit_textarea_1").scrollTop=document.getElementById("edit_textarea_2").scrollTop;
            }
            function textchange(){
                    var op = eq({ value1: document.getElementById("edit_textarea_1").value, value2: document.getElementById("edit_textarea_2").value });
                    document.getElementById("edit_pre_1").innerHTML=op.value1+"\r\n";
                    document.getElementById("edit_pre_2").innerHTML=op.value2+"\r\n";
            }
            function eq(op) {
                if(!op){
                    return op;
                }
                if(!op.value1_style){
                    op.value1_style="background-color:#FEC8C8;";
                }
                if(!op.value2_style){
                    op.value2_style="background-color:#FEC8C8;";
                }
                if(!op.eq_min){
                    op.eq_min=3;
                }
                if(!op.eq_index){
                    op.eq_index=5;
                }
                if (!op.value1 || !op.value2) {
                    return op;
                }
                var ps = {
                    v1_i: 0,
                    v1_new_value: "",
                    v2_i: 0,
                    v2_new_value: ""
                };
                while (ps.v1_i < op.value1.length && ps.v2_i < op.value2.length) {
                    if (op.value1[ps.v1_i] == op.value2[ps.v2_i]) {
                        ps.v1_new_value += op.value1[ps.v1_i].replace(/</g,"<").replace(">",">");
                        ps.v2_new_value += op.value2[ps.v2_i].replace(/</g,"<").replace(">",">");
                        ps.v1_i += 1;
                        ps.v2_i += 1;
                        if (ps.v1_i >= op.value1.length) {
                            ps.v2_new_value += "<span style='" + op.value2_style + "'>" + op.value2.substr(ps.v2_i).replace(/</g,"<").replace(">",">") + "</span>";
                            break;
                        }
                        if (ps.v2_i >= op.value2.length) {
                            ps.v1_new_value += "<span style='" + op.value1_style + "'>" + op.value1.substr(ps.v1_i).replace(/</g,"<").replace(">",">") + "</span>";
                            break;
                        }
                    } else {
                        ps.v1_index = ps.v1_i + 1;
                        ps.v1_eq_length = 0;
                        ps.v1_eq_max = 0;
                        ps.v1_start = ps.v1_i + 1;
                        while (ps.v1_index < op.value1.length) {
                            if (op.value1[ps.v1_index] == op.value2[ps.v2_i + ps.v1_eq_length]) {
                                ps.v1_eq_length += 1;
                            } else if (ps.v1_eq_length > 0) {
                                if (ps.v1_eq_max < ps.v1_eq_length) {
                                    ps.v1_eq_max = ps.v1_eq_length;
                                    ps.v1_start = ps.v1_index - ps.v1_eq_length;
                                }
                                ps.v1_eq_length = 0;
                                break;//只寻找最近的
                            }
                            ps.v1_index += 1;
                        }
                        if (ps.v1_eq_max < ps.v1_eq_length) {
                            ps.v1_eq_max = ps.v1_eq_length;
                            ps.v1_start = ps.v1_index - ps.v1_eq_length;
                        }

                        ps.v2_index = ps.v2_i + 1;
                        ps.v2_eq_length = 0;
                        ps.v2_eq_max = 0;
                        ps.v2_start = ps.v2_i + 1;
                        while (ps.v2_index < op.value2.length) {
                            if (op.value2[ps.v2_index] == op.value1[ps.v1_i + ps.v2_eq_length]) {
                                ps.v2_eq_length += 1;
                            } else if (ps.v2_eq_length > 0) {
                                if (ps.v2_eq_max < ps.v2_eq_length) {
                                    ps.v2_eq_max = ps.v2_eq_length;
                                    ps.v2_start = ps.v2_index - ps.v2_eq_length;
                                }
                                ps.v1_eq_length = 0;
                                break;//只寻找最近的
                            }
                            ps.v2_index += 1;
                        }
                        if (ps.v2_eq_max < ps.v2_eq_length) {
                            ps.v2_eq_max = ps.v2_eq_length;
                            ps.v2_start = ps.v2_index - ps.v2_eq_length;
                        }
                        if (ps.v1_eq_max < op.eq_min && ps.v1_start - ps.v1_i > op.eq_index) {
                            ps.v1_eq_max = 0;
                        }
                        if (ps.v2_eq_max < op.eq_min && ps.v2_start - ps.v2_i > op.eq_index) {
                            ps.v2_eq_max = 0;
                        }
                        if ((ps.v1_eq_max == 0 && ps.v2_eq_max == 0)) {
                            ps.v1_new_value += "<span style='" + op.value1_style + "'>" + op.value1[ps.v1_i].replace(/</g,"<").replace(">",">") + "</span>";
                            ps.v2_new_value += "<span style='" + op.value2_style + "'>" + op.value2[ps.v2_i].replace(/</g,"<").replace(">",">") + "</span>";
                            ps.v1_i += 1;
                            ps.v2_i += 1;

                            if (ps.v1_i >= op.value1.length) {
                                ps.v2_new_value += "<span style='" + op.value2_style + "'>" + op.value2.substr(ps.v2_i).replace(/</g,"<").replace(">",">") + "</span>";
                                break;
                            }
                            if (ps.v2_i >= op.value2.length) {
                                ps.v1_new_value += "<span style='" + op.value1_style + "'>" + op.value1.substr(ps.v1_i).replace(/</g,"<").replace(">",">") + "</span>";
                                break;
                            }
                        } else if (ps.v1_eq_max > ps.v2_eq_max) {
                            ps.v1_new_value += "<span style='" + op.value1_style + "'>" + op.value1.substr(ps.v1_i, ps.v1_start - ps.v1_i).replace(/</g,"<").replace(">",">") + "</span>";
                            ps.v1_i = ps.v1_start;
                        } else {
                            ps.v2_new_value += "<span style='" + op.value2_style + "'>" + op.value2.substr(ps.v2_i, ps.v2_start - ps.v2_i).replace(/</g,"<").replace(">",">") + "</span>";
                            ps.v2_i = ps.v2_start;
                        }
                    }
                }
                op.value1 = ps.v1_new_value;
                op.value2 = ps.v2_new_value;
                return op;
            }
            function settextheight(){
                var heigth=(document.documentElement.clientHeight-6)+"px"
                document.getElementById("edit_pre_1").style.height=heigth;
                document.getElementById("edit_textarea_1").style.height=heigth;
                document.getElementById("edit_pre_2").style.height=heigth;
                document.getElementById("edit_textarea_2").style.height=heigth;
            }
            window.onload=function(){
                settextheight();
                window.onresize=function(){
                    settextheight();
                }
            }
        </script>
    </body>
</html>

纯JS文本比较工具源码
时间: 2024-08-01 14:06:45

纯JS文本比较工具的相关文章

纯JavaScript代码实现文本比较工具_javascript技巧

先上效果图: 代码如下所示: 把源码保存为html格式的文件就可以直接运行了 <!doctype html> <html> <head> <title>文本比较工具</title> <style type="text/css"> *{padding:px;margin:px;} html,body{ overflow-y: hidden; } .edit_div{ border: px solid #CCCCCC;

纯js写的分页表格数据为json串

 这篇文章主要介绍了纯js写的分页,表格数据为json串,需要的朋友可以参考下 什么也不说了,直接上代码:   代码如下: <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  <%@ taglib prefix="s" uri="/struts-tags" %>  <%  String pa

纯js客服插件集qq、旺旺、skype、百度hi、msn

原文 纯js客服插件集qq.旺旺.skype.百度hi.msn 客服插件,集qq.旺旺.skype.百度hi.msn 等 即时通讯工具,并可自己添加支持的通讯工具,极简主义,用法自己琢磨.我的博客 http://www.qiling.org <script> //在线客服插件 powered by casejs 极简主义 http://www.mlrzw.cn function CaseService(caseServiceConfig) { this.config = caseService

纯js实现重发验证码按钮倒数功能

  这篇文章主要介绍了纯js实现重发验证码按钮倒数功能,本文整理了两个实现代码,需要的朋友可以参考下 代码一: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml

纯JS实现动态时间显示代码

 本篇文章主要是对纯JS实现动态时间显示的代码进行了介绍,需要的朋友可以过来参考下,希望对大家有所帮助 代码如下: <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>无标题文档</title

纯JS实现根据CSS的class选择DOM

 这篇文章主要介绍了纯JS实现根据CSS的class选择DOM,需要的朋友可以参考下 // 网上参考的,自己修改了一部分  // 代码如下,纯JS,要求浏览器支持 getElementsByClassName 方法   代码如下: <script type="text/javascript">  function getElementsByClassName(classname,node){  node = node || window.document;  if(node

javascript-js如何获取本机mac地址?求各位高手指点 最好是纯js的 求各位高手给出关键代码

问题描述 js如何获取本机mac地址?求各位高手指点 最好是纯js的 求各位高手给出关键代码 如何利用js获取本机mac地址 求各位高手能给出关键代码 最好是不需要用c直接纯js实现 解决方案 不知道你为什么要获得mac地址,http协议基于tcp/ip,你服务器得到mac也没用. 为了软件加密授权?客户端js获得mac,再提交服务器,这个又很容易伪造. 而且mac地址的获取在不同平台上又不同,不是web标准.你希望你的网站只能在pc+ie上运行?不支持所有别的浏览器?那你还要web做什么. 解

纯js实现网页返回顶部功能(万能的兼容目前所有浏览器)

纯js实现网页返回顶部功能(万能的兼容目前所有浏览器) 在web2.0时代,越来越多的网站如雨后春笋般的冒了出来.而且这些网站提供了很多我们常见的功能.如:返回顶部等等小特性. 那么这些功能是如何实现的呢.这里将为大家提供一些快速使用的万能代码. (function() { var btnId = '__gotop'; var isIE = !!window.ActiveXObject && /msie (\d)/i.test(navigator.userAgent) ? RegExp['

前端-急问:如何使用ajax动态获取后台数据后将数据利用chart.js画图工具显示曲线?

问题描述 急问:如何使用ajax动态获取后台数据后将数据利用chart.js画图工具显示曲线? 20C 刚刚学习ajax,不是很了解.假如后台数据已经以json类型存在,在前端如何使用ajax获取后台的json数据?求给出具体些的完整代码供参考...还有请问如果数据是一系列二位数组(如[a1b1][a2b2]......)那么如何使用chart.js 将其显示出来?还请各位大神指教! 解决方案 ThinkPHP定时ajax获取后台数据,使用javascript动态修改前端页面的表格来显示数据 解