有谁知道一个很好的一小段代码,它将预填充我的用户并将文本用户名和密码传递给输入框。我已经看到如此jscript会这样做,但我正在寻找几行代码的东西。预填充输入文本框
类似搜索文本是如何在搜索框中向上顶右侧
有谁知道一个很好的一小段代码,它将预填充我的用户并将文本用户名和密码传递给输入框。我已经看到如此jscript会这样做,但我正在寻找几行代码的东西。预填充输入文本框
类似搜索文本是如何在搜索框中向上顶右侧
您可以使用以下方法:
<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'"/>
对于支持HTML5的浏览器,添加占位符=“搜索”会自动添加预填充文字。
在页面顶部的搜索文本框中有:
<input type="text" placeholder="search" >
您可以使用HTML5垫片,使这个向后兼容。例如: http://kamikazemusic.com/web-development/revisiting-html5-placeholder-fixes/
...这是标准的HTML。顺便说一句,随意将字符串'用户名'和'密码'外化为常量,和/或任何其他必要的重构。 – Saket