我有兴趣创建一个 javascript 切换功能来高度区分行。
我已经看到一些可以评估文本差异的函数——但我认为它需要遍历每一行——并且在差异上在不同的位周围应用一个跨度标签——然后如果切换被设置回来则删除跨度. Javascript 比较字符串并得到最终差异——但可能有超过 2 个列
var s1 = "The quick brown fox",
s2 = "The quick brown fox jumped over the fence",
string1 = new Array(),
string2 = new Array(),
diff = new Array(),
longString;
string1 = s1.split(" ");
string2 = s2.split(" ");
if (s1.length > s2.length) {
longString = string1;
} else {
longString = string2;
}
for (x = 0; x < longString.length; x++) {
if (string1[x] != string2[x]) {
diff.push(string2[x]);
}
}
document.write("The difference in the strings is " + diff.join(" "));
这是一个与 npm 不同的演示——http: //jsfiddle.net/8c4nt2e1/1/
Cats萌萌
相关分类