最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

純JavaScript代碼實現(xiàn)文本比較工具

 更新時間:2016年02月17日 11:06:47   作者:raotf  
之前項目需求需要寫一個純js文本比較工具,在此小編把代碼分享在腳本之家平臺供大家參考

之前項目需求需要寫一個純js文本比較工具,在此小編把代碼分享在腳本之家平臺供大家參考,算法有待優(yōu)化,本文寫的不好還請見諒。

先上效果圖:

代碼如下所示:

把源碼保存為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,"&lt;").replace(">","&gt;");
ps.v_new_value += op.value[ps.v_i].replace(/</g,"&lt;").replace(">","&gt;");
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,"&lt;").replace(">","&gt;") + "</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,"&lt;").replace(">","&gt;") + "</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,"&lt;").replace(">","&gt;") + "</span>";
ps.v_new_value += "<span style='" + op.value_style + "'>" + op.value[ps.v_i].replace(/</g,"&lt;").replace(">","&gt;") + "</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,"&lt;").replace(">","&gt;") + "</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,"&lt;").replace(">","&gt;") + "</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,"&lt;").replace(">","&gt;") + "</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,"&lt;").replace(">","&gt;") + "</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代碼實現(xiàn)文本比較工具,希望對大家有所幫助!

相關文章

  • javascript URL錨點取值方法

    javascript URL錨點取值方法

    url取值的比較不錯的思路,一般都是這樣的話,或直接先將?替換成統(tǒng)計分隔符,js數(shù)組一定要特別熟悉。
    2009-02-02
  • JavaScript獲取路徑設計源碼

    JavaScript獲取路徑設計源碼

    JavaScript獲取路徑的方法有很多,網(wǎng)上也可以搜索的到,下面本例也要為大家講解下,有源碼及結果截圖
    2014-05-05
  • setTimeout()遞歸調用不加引號出錯的解決方法

    setTimeout()遞歸調用不加引號出錯的解決方法

    用了setTimeout()想實現(xiàn)遞歸調用,如果第一個參數(shù)不加引號的話,就會出錯,下面與大家分享下該如何解決
    2014-09-09
  • js for終止循環(huán) 跳出多層循環(huán)

    js for終止循環(huán) 跳出多層循環(huán)

    這篇文章主要介紹了js for等循環(huán) 跳出多層循環(huán),終止循環(huán)執(zhí)行的方法,需要的朋友可以參考下
    2018-10-10
  • javascript實現(xiàn)復選框全選或反選

    javascript實現(xiàn)復選框全選或反選

    這篇文章主要為大家詳細介紹了javascript實現(xiàn)復選框全選或反選,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-02-02
  • JS實現(xiàn)的數(shù)字格式化功能示例

    JS實現(xiàn)的數(shù)字格式化功能示例

    這篇文章主要介紹了JS實現(xiàn)的數(shù)字格式化功能,結合實例形式分析了javascript針對數(shù)字與字符的相關運算處理技巧,需要的朋友可以參考下
    2017-02-02
  • JS實現(xiàn)iframe自適應高度的方法示例

    JS實現(xiàn)iframe自適應高度的方法示例

    這篇文章主要介紹了JS實現(xiàn)iframe自適應高度的方法,結合實例形式分析了JS實現(xiàn)iframe高度自適應的實現(xiàn)技巧,并給出了項目示例供大家參考,需要的朋友可以參考下
    2017-01-01
  • JS使用Promise控制請求并發(fā)數(shù)

    JS使用Promise控制請求并發(fā)數(shù)

    現(xiàn)在面試過程當中 ,手寫題必然是少不了的,其中碰到比較多的無非就是當屬 請求并發(fā)控制了,所以本文為大家整理了JS使用Promise控制請求并發(fā)數(shù)的示例代碼,希望對大家有所幫助
    2023-05-05
  • javascript+jQuery實現(xiàn)360開機時間顯示效果

    javascript+jQuery實現(xiàn)360開機時間顯示效果

    這篇文章主要介紹了javascript+jQuery實現(xiàn)360開機時間顯示效果,在文中給大家提到了js實現(xiàn)時間倒計時的代碼,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2017-11-11
  • JS+Vue實現(xiàn)三級全選單選

    JS+Vue實現(xiàn)三級全選單選

    這篇文章主要為大家詳細介紹了JS+Vue實現(xiàn)三級全選單選,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-07-07

最新評論

射阳县| 宣威市| 威远县| 阿拉善盟| 镇远县| 蛟河市| 沈阳市| 汨罗市| 深圳市| 茌平县| 长汀县| 洞口县| 绩溪县| 崇礼县| 罗甸县| 宁都县| 壶关县| 大庆市| 平凉市| 清水河县| 望都县| 米易县| 临朐县| 板桥市| 黎平县| 清水河县| 贵南县| 凌云县| 固阳县| 恩施市| 怀安县| 嘉鱼县| 安塞县| 黎川县| 元朗区| 章丘市| 资源县| 石河子市| 淮滨县| 绿春县| 榆树市|