纯JS文本比较工具
2023-09-14 08:56:48 时间
pre id="edit_pre_1" /pre
textarea id="edit_textarea_1" onscroll="test1_scroll()" oninput="textchange()" onpropertychange="textchange()" /textarea
/div
/td
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_ ;
if(!op.value2_style){
op.value2_ ;
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文本比较工具源码
相关文章
- Js排序算法_js 排序算法
- js定时器与延时器_JS做定时器倒计时
- js中四舍五入的方法_JS取整
- App逆向案例 X嘟牛 - Frida监听 & WT-JS工具还原(一)
- 原生 JS 实现 HTML 转 Markdown,以及其实现逻辑(html2md.js 或 html2markdown.js)
- 【源码】PDF.js批注注释插件库(纯JS). 创建和保存PDF批注(PDF 高亮/签名/插图/截屏/文本框/画笔/多边形)-pdf.js
- js调试工具console.log()方法查看js代码的执行情况详解编程语言
- 使用 Node.js 构建交互式命令行工具
- 用Python和JS实现的Web SSH工具,真香!
- Oracle中JS的优势让数据库性能提升(oracle中 js)
- 脚本之家贴图转换+转贴工具用到的js代码超级推荐
- 一个用js实现的页内搜索代码
- 自己的js工具_Form封装
- Js随机数产生6位数字
- CSS和JS标签style属性对照表(方便js开发的朋友)
- JS操作图片(增,删,改)例子
- js改变img标签的src属性在IE下没反应的解决方法
- js抽奖实现随机抽奖代码效果
- JS弹出层的显示与隐藏示例代码
- js如何获取object类型里的键值
- js调试工具console.log()方法查看js代码的执行情况
- JS使用for循环遍历子节点查找元素