输入(密码)onblr javascript bug internetexplorer 7和8

input(password) onblur javascript bug internet explorer 7 and 8

本文关键字:internetexplorer bug javascript 密码 onblr 输入      更新时间:2023-09-26

这是我输入的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中工作,它显示文本而不是密码(星号)或隐藏值。

placeholder属性用于此行为。这样,在现代浏览器中就不需要任何支持此属性的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">

我用jQuery插件格式制作了一个@placeholder polyfill,您可以如下使用:

$('input').placeholder();

下面是一个演示:http://mathiasbynens.be/demo/placeholder正如您所看到的,它可以很好地处理textarea和密码输入。