也許,是的。
你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' –