2015-06-20 71 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); 
}