`
Lamp兄弟
  • 浏览: 16994 次
  • 性别: Icon_minigender_1
  • 来自: 西安
社区版块
存档分类
最新评论

javascript文本比较

阅读更多

库文件到附件中下载。

实例代码如下

<script type="text/javascript" src="/diffview.js"></script>
<link rel="stylesheet" type="text/css" href="/diffview.css"/>
<script type="text/javascript" src="/jsdiff.js"></script>
<script type="text/javascript" src="/difflib.js"></script>

 

<script>
window.onload = function(){
	var input = document.getElementById("input");
	var output = document.getElementById("output");
	var form = document.getElementById("form");

	input.value = "Two pieces to the puzzle\n\nDiff script by snowtide\nSnowtide script has no word diff\nCombine the two and it rocks!";
	output.value = "Two pieces to the puzzle\n\nDiff script by john resig\nResig script has no line by line diff\n\nCombine the two and it rocks!";

	form.onsubmit = function(e){
		if (e) e.preventDefault();
		if (typeof event != "undefined") event.returnValue = false;

		diffUsingJS ();
		return false;
	};
};

function diffUsingJS () {
	var base = difflib.stringAsLines(document.getElementById("input").value);
	var newtxt = difflib.stringAsLines(document.getElementById("output").value);
	var sm = new difflib.SequenceMatcher(base, newtxt);
	var opcodes = sm.get_opcodes();
	var diffoutputdiv = document.getElementById("diffoutput");
	while (diffoutputdiv.firstChild) diffoutputdiv.removeChild(diffoutputdiv.firstChild);
	var contextSize = 3;
	contextSize = contextSize ? contextSize : null;
	diffoutputdiv.appendChild(diffview.buildView({ baseTextLines:base,
												   newTextLines:newtxt,
												   opcodes:opcodes,
												   baseTextName:"Old",
												   newTextName:"New",
												   contextSize:contextSize,
												   viewType: 0}));
	
}
</script>
<div style="float:left;width:50%">
<form id="form"><b>Old:</b><br/>
<textarea cols="60" rows="10" id="input"></textarea><br/>

<div>
<b>New:</b><br/>
<textarea cols="60" rows="10" id="output"></textarea>
</div>
<input type="submit" value="Diff"/></form><br/>
</div>

<div id="diffoutput" style="clear:both;width:100%;"> </div>
<div style="clear:both;">&nbsp;</div>

 效果如下图



 

 

  • 大小: 21.4 KB
分享到:
评论
3 楼 yvhiwssn 2015-06-09  
     :idea:
2 楼 hover_hs 2014-01-07  
为什么运行出来,都是黄颜色呢
1 楼 霁月长辉 2013-06-25  
为什么运行没有上面那种效果呢

相关推荐

Global site tag (gtag.js) - Google Analytics