let diffUntil = new diff_match_patch();
function diffText(diffType) { // 3 个对比调用一个方法,传入不同参数而已
let text1 = document.getElementById('text1').value; // 文本域 1
let text2 = document.getElementById('text2').value; // 文本域 2
let editCost = document.getElementById('editCost').value || 4; // 没填的时候默认为 4
let d = diffUntil.diff_main(text1, text2); // 对比
if (diffType ==='semantic') {
diffUntil.diff_cleanupSemantic(d);
}
if (diffType ==='efficiency'){
diffUntil.Diff_EditCost = parseInt(editCost);
diffUntil.diff_cleanupEfficiency(d);
}
let diffhtml = diffUntil.diff_prettyHtml(d); // 转化为 html
document.getElementById('result').innerHTML = diffHtml;
}
来源: http://www.jianshu.com/p/5239fe681fe3