简体   繁体   English

文本输入框,框内有标签

[英]Text input box with label inside the box

I'm experimenting with a variety of techniques of putting the label for the text input box inside the box. 我正在尝试将文本输入框的标签放在框内的各种技术。 Then the label disappears as you start typing, leaving the label in place for as long as possible before you start typing. 然后,标签会在您开始键入时消失,并在开始键入之前将标签保留尽可能长的时间。 Basecamp (basecamp.com) has a really good effect that I would like to emulate, but not sure how they are doing it. 我想效仿Basecamp(basecamp.com),但效果不确定,但效果不佳。

When you click a text box that has a label inside it, the label doesn't go away until you start typing. 当您单击其中带有标签的文本框时,该标签直到开始输入时才会消失。 That is relatively easy to script, with one catch: if you click inside the text box, your cursor could appear anywhere inside the label text... at the beginning, in the middle, at the end. 这是相对容易编写的脚本,只有一个要点:如果在文本框中单击,则光标可能会出现在标签文本内的任何位置……在开头,中间,结尾。 Or, it could even select one or more characters instead of just inserting the cursor somewhere. 或者,它甚至可以选择一个或多个字符,而不仅仅是将光标插入某处。

On the Basecamp site, you cannot select any of the label text. 在Basecamp网站上,您不能选择任何标签文本。 No matter where you click, the cursor appears at the far left (beginning) of the text box. 无论单击何处,光标都会出现在文本框的最左侧(开始)。 Even if you double-click, it won't select the label text. 即使双击,它也不会选择标签文本。

How are they doing this? 他们是如何做到的? Thank you. 谢谢。

Just use the placeholder attribute as mentioned in the comments above. 只需使用上面评论中提到的placeholder属性即可。 A good fall back is https://github.com/mathiasbynens/jquery-placeholder 一个很好的后退是https://github.com/mathiasbynens/jquery-placeholder

Also, if you want to style the placeholder in modern browsers you can use the following: 另外,如果要在现代浏览器中设置占位符的样式,则可以使用以下命令:

::-webkit-input-placeholder {
   color: white; /* or any colour you like */
}

:-moz-placeholder { /* Firefox 18- */
   color: white;
}

::-moz-placeholder {  /* Firefox 19+ */
   color: white;
}

:-ms-input-placeholder {
   color: white;
}

Have you tried using a placeholder like this? 您是否尝试过使用这样的占位符?

<input type="text" id="exampleInput" placeholder="Enter some text">

Edit: 编辑:

Looking at the basecamp's login page for example it seems they avoided the placeholder and did it with CSS using a class of overLabel: 例如,看一下basecamp的登录页面 ,似乎他们避开了占位符,并使用overLabel类使用CSS进行了处理:

<label for="username" class="overlabel" style="">Username or email</label>
<input autocapitalize="off" autocomplete="on" autocorrect="off" class="overlayable"    id="username" name="username" title="Username or email" type="text">

body.login div.login_dialog span.overlay_wrapper label.overlabel {
    position: absolute;
    top: -2px;
    left: 6px;
    z-index: 1;
    color: #999 !important;
    font-size: 18px;
}

Placeholder works just as well IMO. 占位符与IMO一样工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM