2011-09-26 86 views
0

有谁知道一个很好的一小段代码,它将预填充我的用户并将文本用户名和密码传递给输入框。我已经看到如此jscript会这样做,但我正在寻找几行代码的东西。预填充输入文本框

类似搜索文本是如何在搜索框中向上顶右侧

回答

1

您可以使用以下方法:

<input type="text" style="color:#ccc;" value="username" onfocus="this.value = this.value=='username' ? '' : this.value; this.style.color='#000';" onfocusout="this.value = this.value == '' ? this.value = 'username' : this.value; this.value=='username' ? this.style.color='#ccc' : this.style.color='#000'"/> 

<input type="text" style="color:#ccc;" value="password" onfocus="this.value = this.value=='password' ? '' : this.value; this.style.color='#000';" onfocusout="this.value = this.value == '' ? this.value = 'password' : this.value; this.value=='password' ? this.style.color='#ccc' : this.style.color='#000'"/> 
+0

...这是标准的HTML。顺便说一句,随意将字符串'用户名'和'密码'外化为常量,和/或任何其他必要的重构。 – Saket

19

对于支持HTML5的浏览器,添加占位符=“搜索”会自动添加预填充文字。

在页面顶部的搜索文本框中有:

<input type="text" placeholder="search" > 

您可以使用HTML5垫片,使这个向后兼容。例如: http://kamikazemusic.com/web-development/revisiting-html5-placeholder-fixes/

+0

好一个!如果你不知道,@acctman可以通过使用“查看页面源”选项(右键单击此页面上的任何地方)来查看此页面的源代码。 – Saket

+0

这里更先进的数据链来占位符垫片:http://kamikazemusic.com/web-development/revisiting-html5-placeholder-fixes/ 伟大的作品!谢谢 – Wipster