Javascript 在internet explorer 7和8上输入(密码)

Javascript 在internet explorer 7和8上输入(密码),javascript,Javascript,这是我输入的javascript内联代码 <input type="text" value="Please type username here..." onfocus="if (this.value == 'Please type username here...') {this.value = '';}" onblur="if (this.value == '') {this.value = 'Please type username here...';}" name="name1"

这是我输入的javascript内联代码

<input type="text" value="Please type username here..." onfocus="if (this.value == 'Please type username here...') {this.value = '';}" onblur="if (this.value == '') {this.value = 'Please type username here...';}" name="name1" id="username">


它在mozilla、IE9 Chrome中工作,但在IE7和IE8中不工作,它显示文本而不是密码(星号)或隐藏值。

使用
占位符属性进行此行为。这样,在支持此属性的现代浏览器中就不需要任何JavaScript了。然后,您可以使用polyfill使其在IE9和更旧的浏览器中工作

您的HTML将如下所示:

<input type="text" placeholder="e.g. myusername" name="username" id="username">
<input type="password" placeholder="e.g. hunter2" name="password" id="password">

这里有一个演示:正如您所看到的,它可以很好地处理
textarea
s和密码输入。

我已经将它复制并粘贴到一个文件中,并在IE8.0中进行了测试和工作。在IE8和IE7模式下测试,在这两种模式下都可以正常工作。您确定复制了正确的代码行吗?这是一个用户名字段,不是密码。在IE8中没有问题。是的,thnx buddy,我会尝试并肯定会回复结果:)
$('input').placeholder();