无为清净楼资源网 Design By www.qnjia.com
之前项目需求需要写一个纯js文本比较工具,在此小编把代码分享在平台供大家参考,算法有待优化,本文写的不好还请见谅。
先上效果图:
代码如下所示:
把源码保存为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; overflow: auto; position: relative; } .edit_div textarea{ resize:none; background: none repeat scroll transparent; border: none; width: %; height:px; overflow-y: scroll; position: absolute; left: px; top: px; z-index: ; font-size: px; 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: %; height:px; text-align: left; color: #ffffff; z-index: ; font-size: px; } </style> </head> <body> <table style="width:%"> <tr> <td style="width:%"> <div class="edit_div"> <pre id="edit_pre_"></pre> <textarea id="edit_textarea_" onscroll="test_scroll()" oninput="textchange()" onpropertychange="textchange()"></textarea> </div> </td> <td style="width:%"> <div class="edit_div"> <pre id="edit_pre_"></pre> <textarea id="edit_textarea_" onscroll="test_scroll()" oninput="textchange()" onpropertychange="textchange()"></textarea> </div> </td> </tr> </table> <script type="text/javascript"> function test_scroll(){ document.getElementById("edit_pre_").scrollTop=document.getElementById("edit_textarea_").scrollTop; document.getElementById("edit_pre_").scrollTop=document.getElementById("edit_pre_").scrollTop; document.getElementById("edit_textarea_").scrollTop=document.getElementById("edit_textarea_").scrollTop; } function test_scroll(){ document.getElementById("edit_pre_").scrollTop=document.getElementById("edit_textarea_").scrollTop; document.getElementById("edit_pre_").scrollTop=document.getElementById("edit_pre_").scrollTop; document.getElementById("edit_textarea_").scrollTop=document.getElementById("edit_textarea_").scrollTop; } function textchange(){ var op = eq({ value: document.getElementById("edit_textarea_").value, value: document.getElementById("edit_textarea_").value }); document.getElementById("edit_pre_").innerHTML=op.value+"\r\n"; document.getElementById("edit_pre_").innerHTML=op.value+"\r\n"; } function eq(op) { if(!op){ return op; } if(!op.value_style){ op.value_style="background-color:#FECC;"; } if(!op.value_style){ op.value_style="background-color:#FECC;"; } if(!op.eq_min){ op.eq_min=; } if(!op.eq_index){ op.eq_index=; } if (!op.value || !op.value) { return op; } var ps = { v_i: , v_new_value: "", v_i: , v_new_value: "" }; while (ps.v_i < op.value.length && ps.v_i < op.value.length) { if (op.value[ps.v_i] == op.value[ps.v_i]) { ps.v_new_value += op.value[ps.v_i].replace(/</g,"<").replace(">",">"); ps.v_new_value += op.value[ps.v_i].replace(/</g,"<").replace(">",">"); ps.v_i += ; ps.v_i += ; if (ps.v_i >= op.value.length) { ps.v_new_value += "<span style='" + op.value_style + "'>" + op.value.substr(ps.v_i).replace(/</g,"<").replace(">",">") + "</span>"; break; } if (ps.v_i >= op.value.length) { ps.v_new_value += "<span style='" + op.value_style + "'>" + op.value.substr(ps.v_i).replace(/</g,"<").replace(">",">") + "</span>"; break; } } else { ps.v_index = ps.v_i + ; ps.v_eq_length = ; ps.v_eq_max = ; ps.v_start = ps.v_i + ; while (ps.v_index < op.value.length) { if (op.value[ps.v_index] == op.value[ps.v_i + ps.v_eq_length]) { ps.v_eq_length += ; } else if (ps.v_eq_length > ) { if (ps.v_eq_max < ps.v_eq_length) { ps.v_eq_max = ps.v_eq_length; ps.v_start = ps.v_index - ps.v_eq_length; } ps.v_eq_length = ; break;//只寻找最近的 } ps.v_index += ; } if (ps.v_eq_max < ps.v_eq_length) { ps.v_eq_max = ps.v_eq_length; ps.v_start = ps.v_index - ps.v_eq_length; } ps.v_index = ps.v_i + ; ps.v_eq_length = ; ps.v_eq_max = ; ps.v_start = ps.v_i + ; while (ps.v_index < op.value.length) { if (op.value[ps.v_index] == op.value[ps.v_i + ps.v_eq_length]) { ps.v_eq_length += ; } else if (ps.v_eq_length > ) { if (ps.v_eq_max < ps.v_eq_length) { ps.v_eq_max = ps.v_eq_length; ps.v_start = ps.v_index - ps.v_eq_length; } ps.v_eq_length = ; break;//只寻找最近的 } ps.v_index += ; } if (ps.v_eq_max < ps.v_eq_length) { ps.v_eq_max = ps.v_eq_length; ps.v_start = ps.v_index - ps.v_eq_length; } if (ps.v_eq_max < op.eq_min && ps.v_start - ps.v_i > op.eq_index) { ps.v_eq_max = ; } if (ps.v_eq_max < op.eq_min && ps.v_start - ps.v_i > op.eq_index) { ps.v_eq_max = ; } if ((ps.v_eq_max == && ps.v_eq_max == )) { ps.v_new_value += "<span style='" + op.value_style + "'>" + op.value[ps.v_i].replace(/</g,"<").replace(">",">") + "</span>"; ps.v_new_value += "<span style='" + op.value_style + "'>" + op.value[ps.v_i].replace(/</g,"<").replace(">",">") + "</span>"; ps.v_i += ; ps.v_i += ; if (ps.v_i >= op.value.length) { ps.v_new_value += "<span style='" + op.value_style + "'>" + op.value.substr(ps.v_i).replace(/</g,"<").replace(">",">") + "</span>"; break; } if (ps.v_i >= op.value.length) { ps.v_new_value += "<span style='" + op.value_style + "'>" + op.value.substr(ps.v_i).replace(/</g,"<").replace(">",">") + "</span>"; break; } } else if (ps.v_eq_max > ps.v_eq_max) { ps.v_new_value += "<span style='" + op.value_style + "'>" + op.value.substr(ps.v_i, ps.v_start - ps.v_i).replace(/</g,"<").replace(">",">") + "</span>"; ps.v_i = ps.v_start; } else { ps.v_new_value += "<span style='" + op.value_style + "'>" + op.value.substr(ps.v_i, ps.v_start - ps.v_i).replace(/</g,"<").replace(">",">") + "</span>"; ps.v_i = ps.v_start; } } } op.value = ps.v_new_value; op.value = ps.v_new_value; return op; } function settextheight(){ var heigth=(document.documentElement.clientHeight-)+"px" document.getElementById("edit_pre_").style.height=heigth; document.getElementById("edit_textarea_").style.height=heigth; document.getElementById("edit_pre_").style.height=heigth; document.getElementById("edit_textarea_").style.height=heigth; } window.onload=function(){ settextheight(); window.onresize=function(){ settextheight(); } } </script> </body> </html>
以上代码是使用的纯JavaScript代码实现文本比较工具,希望对大家有所帮助!
标签:
js文本比较工具,js文本
无为清净楼资源网 Design By www.qnjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
无为清净楼资源网 Design By www.qnjia.com
暂无评论...
更新日志
2024年11月20日
2024年11月20日
- 陈致逸《幻想乐园 Fantasyland》[320K/MP3][120.54MB]
- 席卷全球最红舞曲《火辣辣DJ[英文版]》[DTS-WAV]
- 群星-席卷全球最红舞曲《火辣辣DJ中文版》【WAV】
- 模拟之声慢刻CD《声入人心[年度发烧人声严选]》[低速原抓WAV+CUE]
- 陈致逸《幻想乐园 Fantasyland》[FLAC/分轨][554.27MB]
- Rhymist / LusciousBB《年轮》[320K/MP3][76.52MB]
- Rhymist / LusciousBB《年轮》[FLAC/分轨][410.02MB]
- 群星《歌手2024 第11期》[320K/MP3][93.88MB]
- 群星《歌手2024 第11期》[FLAC/分轨][496.06MB]
- 群星《国风超有戏 第7期》[320K/MP3][30.73MB]
- 模拟之声慢刻CD《试音天品8[女声低音炮]》[低速原抓WAV+CUE]
- 群星《抖烧第三季DSD》[低速原抓WAV+CUE]
- [ABC]蔡琴《百万琴歌[6N纯银镀膜]》[低速原抓WAV+CUE]
- 群星《国风超有戏 第7期》[FLAC/分轨][147.99MB]
- 群星《闪光的夏天 第3期》[320K/MP3][61.94MB]