2012-02-13 75 views
2

这里是我输入的JavaScript嵌入代码输入(密码)的onblur的JavaScript错误Internet Explorer 7和8

<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浏览器,但在IE7和IE8不工作,它显示文本,而不是密码(星号)或隐藏值。

+0

我已将它复制并粘贴到一个文件中,并在IE8中进行了测试和工作。 – 2012-02-13 08:09:38

+0

同样。在IE8和IE7模式下测试,两者都可以正常工作。你确定你复制了正确的代码行吗?这是一个用户名字段,而不是密码。 – Feysal 2012-02-13 08:11:18

+1

IE8中的[this jsfiddle](http://jsfiddle.net/SZ9sP/)没有问题。 – scessor 2012-02-13 08:11:35

回答

1

对此行为使用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"> 

我做a @placeholder polyfill in jQuery plugin format,你可以为使用如下:

$('input').placeholder(); 

这里有一个演示:http://mathiasbynens.be/demo/placeholder正如你所看到的,它处理textarea S和密码投入就好了。

+0

是的thnx哥们,我会试试,一定会回复结果:) – 2012-02-14 05:47:03