2015-06-20 89 views
4

我遇到了問題。我實際上爲自己做了一個博客,一切都很順利。但是,有一次我想用自己的輸入做一些很酷的事情,那麼如果我可以一個一個地移除佔位符字符呢?按字符刪除字符

var run = 0; 
var inte; 
function removet(obj) { 
    run = 0; 
    setInterval(function() { 
     if(run > 8) { 
      clearInterval(); 
     } 
     else { 
      stri = obj.placeholder; 
      stri = stri.substring(0, stri.length - 1); 
      obj.placeholder = stri; 
      run++; 
     } 
    }, 22, obj); 
} 

而且,它工作! ...但。我有一個小問題。 這裏是我的HTML:

<input type="text" placeholder="Username" onblur="this.placeholder = 'Username'" onfocus="removet(this)"> 
<input type="password" placeholder="Password" onblur="this.placeholder = 'Password'" onfocus="removet(this)"> 
<button type="submit" id="login-button">Login</button> 

刪除USERNAME工作正常,但是當我嘗試與密碼......這兩個在一半刪除。爲什麼?我的錯誤在哪裏?

+0

@Teemu我只是在想同樣的事情,但在我腦子不太清楚的說明。做出答案! – Sebivor

回答

1

嘗試並不清楚所有間隔

function removet(obj) { 
    run = 0; 
    var k= setInterval(function() { 
     if(run > 8) { 
      clearInterval(k); 
     } 
     else { 
      stri = obj.placeholder; 
      stri = stri.substring(0, stri.length - 1); 
      obj.placeholder = stri; 
      run++; 
     } 
    }, 22, obj); 
}