最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Pre-fill input text box - Stack Overflow

programmeradmin3浏览0评论

Does anyone know of a good small piece of code that will prefill my user and pass input box with the text username and password. I've seen so jscript that will do it but i'm looking for something that is a few lines a code.

similar to how the search text is in the search box up top on the right

Does anyone know of a good small piece of code that will prefill my user and pass input box with the text username and password. I've seen so jscript that will do it but i'm looking for something that is a few lines a code.

similar to how the search text is in the search box up top on the right

Share Improve this question asked Sep 26, 2011 at 4:19 acctmanacctman 4,34930 gold badges103 silver badges147 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 18

For browsers that support HTML5, adding placeholder="search" will add the prefill text automatically.

The search text box at the top of the page has:

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

You can use an HTML5 shim to make this backwards compatible. Example: http://kamikazemusic.com/web-development/revisiting-html5-placeholder-fixes/

You can use the following:

<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)

  1. 暂无评论