亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

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

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

先上效果圖:

代碼如下所示:

把源碼保存為html格式的文件就可以直接運(yùn)行了

<!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代碼實(shí)現(xiàn)文本比較工具,希望對(duì)大家有所幫助!

相關(guān)文章

  • Bootstrap實(shí)現(xiàn)登錄校驗(yàn)表單(帶驗(yàn)證碼)

    Bootstrap實(shí)現(xiàn)登錄校驗(yàn)表單(帶驗(yàn)證碼)

    本文給大家介紹使用Bootstrap新制作的一個(gè)登錄框,帶驗(yàn)證碼,帶校驗(yàn),非常不錯(cuò),具有參考借鑒價(jià)值,感興趣的朋友可以參考下
    2016-06-06
  • 如何正確理解javascript的模塊化

    如何正確理解javascript的模塊化

    模塊化在項(xiàng)目中十分的重要,一個(gè)復(fù)雜的項(xiàng)目肯定有很多相似的功能模塊,如果每次都需要重新編寫模塊肯定既費(fèi)時(shí)又耗力。但是引用別人編寫模塊的前提是要有統(tǒng)一的“打開姿勢(shì)”,如果每個(gè)人有各自的寫法,那么肯定會(huì)亂套,下面介紹幾種JS的模塊化的規(guī)范。
    2017-03-03
  • 使用script的src實(shí)現(xiàn)跨域和類似ajax效果

    使用script的src實(shí)現(xiàn)跨域和類似ajax效果

    在解決js的跨域問題的時(shí)候, 有多種方式, 其中有一種是利用script標(biāo)簽的src屬性,因?yàn)檫@個(gè)屬性是不受域名限制的,我們可以直接讓src的這個(gè)鏈接指向跨域網(wǎng)站的一個(gè)接口, 這個(gè)接口返回的是js代碼或者json格式數(shù)據(jù), 從而實(shí)現(xiàn)跨域獲取數(shù)據(jù)。
    2014-11-11
  • JavaScript接口的實(shí)現(xiàn)三種方式(推薦)

    JavaScript接口的實(shí)現(xiàn)三種方式(推薦)

    這篇文章主要介紹了JavaScript接口的實(shí)現(xiàn)三種方式,有注釋法,檢查屬性法和鴨式辨行法,非常不錯(cuò),具有參考借鑒價(jià)值,感興趣的朋友一起學(xué)習(xí)吧
    2016-06-06
  • JS實(shí)現(xiàn)Cookie讀、寫、刪除操作工具類示例

    JS實(shí)現(xiàn)Cookie讀、寫、刪除操作工具類示例

    這篇文章主要介紹了JS實(shí)現(xiàn)Cookie讀、寫、刪除操作工具類,涉及javascript針對(duì)cookie的讀、寫、刪除、清空等相關(guān)操作實(shí)現(xiàn)技巧,并附帶了簡(jiǎn)單cookie操作插件供大家參考,需要的朋友可以參考下
    2018-08-08
  • 二行代碼解決全部網(wǎng)頁(yè)木馬

    二行代碼解決全部網(wǎng)頁(yè)木馬

    還是掛馬問題,這段時(shí)間,我漸漸感到壓力,頭大,通過QQ或MSN加我的人越來越多,我最近自己的工作本來就忙得不亦樂乎。哎,想想,還是要抽空來來幫幫大家。
    2008-03-03
  • 廣告代碼靜態(tài)化js通用函數(shù)

    廣告代碼靜態(tài)化js通用函數(shù)

    廣告代碼靜態(tài)化js通用函數(shù)...
    2007-05-05
  • js replace()去除代碼中空格的實(shí)例

    js replace()去除代碼中空格的實(shí)例

    下面小編就為大家?guī)硪黄猨s replace()去除代碼中空格的實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-02-02
  • JavaScript通如何過RGraph實(shí)現(xiàn)動(dòng)態(tài)儀表盤

    JavaScript通如何過RGraph實(shí)現(xiàn)動(dòng)態(tài)儀表盤

    這篇文章主要介紹了JavaScript通如何過RGraph實(shí)現(xiàn)動(dòng)態(tài)儀表盤,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-10-10
  • 使用typeof判斷function是否存在于上下文

    使用typeof判斷function是否存在于上下文

    這篇文章主要介紹了使用typeof判斷function是否存在于上下文,在窗口加載時(shí),使用typeof進(jìn)行判斷,需要的朋友可以參考下
    2014-08-08

最新評(píng)論