问题解决了,但我遇到了另一个问题。下面是与修复的小提琴:http://jsfiddle.net/Axvgf/
这里的改变方法:
function colorize(text, pos) {
var i = 0, current_times = 0;
var startc = '(', endc = ')';
var current = -1;
var entities = {'>': '>','<':'<'};
var p2 = 0;
var regex = new RegExp(Object.keys(entities).join("|"),'g');
var converted = text.replace(regex, function(x, j) {
if(pos > j) p2 += entities[x].length - 1;
return entities[x];
});
pos += p2;
var parens = [], indices = [], o = {};
var newText = converted.replace(/((?:\\)*)([()])/g, function(full, escape, x, idx) {
var len = escape.split(/\\/g).length - 1;
if (len % 2 == 0) {
indices.push(idx);
if (x == startc) ++i;
o[idx] = { selected: false, type: x, depth: i, idx: idx, pair: -1, extra: escape };
if (idx == pos) o[idx].selected = true;
if (x == startc) parens.push(idx);
else {
if (parens.length > 0) {
var p = parens.pop();
o[idx].pair = p;
if (o[p].selected) o[idx].selected = true;
o[p].pair = idx;
if (o[idx].selected) o[p].selected = true;
}
--i
}
}
});
newtext = converted;
indices = indices.sort(function(x,y) { return Number(y) - Number(x); });
indices.forEach(function(i) {
newtext = newtext.substr(0,i) + o[i].extra +
"<span class='" + (o[i].pair == -1 ? "unmatched " : "paren_" + (o[i].depth % 5)) +
(o[i].selected ? " selected_paren": "") + "'>" + o[i].type + "</span>" +
newtext.substr(i + 1 + o[i].extra.length)
});
return newtext;
}
你应该看看jsfiddles源代码,看看他们是如何做到这一点。你的问题太宽泛了。 – goat
@rambocoder:它是如何广泛的?我已经详细说明了它的各个方面。我试过的,我想实现的。 –
建议您将演示范围缩小到较小的组件,使用较少的代码专注于特定的难度区域,并删除不会导致问题的任何非关键代码 – charlietfl