也许,是的。
你cont.indexOf()
测试应该测试>= 0
,由于未找到该函数返回-1,其评估真正并会导致循环再绕过去。
它现在只会终止,如果cont
开始curstring
。
根据其他答案,您还需要在循环内覆盖cont
。
function replace() {
var curstring = "twitter: ";
var str = document.getElementById('feeds');
var cont = str.innerHTML;
var old = cont;
// NB: indexOf() returns -1 on failure, so you
// must compare against that,
while (cont.indexOf(curstring) >= 0) {
cont = cont.replace(curstring, "TWIMG ");
}
// taken outside the loop so we don't modify the DOM
// over and over if the match is repeated - only update
// the DOM if the string got changed
if (cont !== old) {
str.innerHTML = cont;
}
}
你试过用调试器试过吗? – fvu
您可能想要检查您的TAB键是否正在工作......':p' –