sup*_*per 10 html javascript css
我有这样的输入:
<input value="My text" placeholder="Placeholder">
Run Code Online (Sandbox Code Playgroud)
当我在输入中输入内容时,占位符文本将消失,这是非常明显的.
现在,我想要做的是我希望占位符文本在用户输入时保留,以便您可以将占位符文本看作原始文本后面的背景文本:

编辑:我也希望能够使用JavaScript更改背景文本.
Gia*_*vas 11
通过CSS轻松实现更好的解决方案.看看:http://jsfiddle.net/csdtesting/wbqq129q/


码:
#login {
font-size: 12px;
margin: 0 auto;
width: 700px;
}
#login li {
float: left;
list-style: none;
margin-left: 30px;
position: relative;
}
#login li:first-child {
margin-left: 0;
}
label {
line-height: 40px;
position: absolute;
right: 120px;
top: 0;
bottom: 0;
-moz-transition: 0.3s right ease;
-ms-transition: 0.3s right ease;
-o-transition: 0.3s right ease;
-webkit-transition: 0.3s right ease;
transition: 0.3s right ease;
z-index: 0
}
input {
color: transparent;
font-size: 12px;
height: 35px;
-moz-border-radius: 3px;
-webkit-border-radius: 3px;
border-radius: 3px;
-moz-transition: 0.3s all ease;
-ms-transition: 0.3s all ease;
-o-transition: 0.3s all ease;
-webkit-transition: 0.3s all ease;
transition: 0.3s all ease;
}
input[type="email"],
input[type="password"] {
border: 1px solid #ccc;
height: 35px;
padding: 0 10px;
width: 240px;
position: relative;
-moz-box-shadow: inset 0 0 10px rgba(0, 0, 0, .06);
-webkit-box-shadow: inset 0 0 10px rgba(0, 0, 0, .06);
box-shadow: inset 0 0 10px rgba(0, 0, 0, .06);
z-index: 2;
}
input[type="email"] {
color: rgba(47, 130, 194, .8);
}
/* Placeholder */
input[type="email"]:-moz-placeholder {
color: rgba(47, 130, 194, .6);
}
input[type="email"]:-ms-input-placeholder {
color: rgba(47, 130, 194, .6);
}
input[type="email"]::-webkit-input-placeholder {
color: rgba(47, 130, 194, .6);
}
/* Label */
input[type="email"] + label {
color: rgb(47, 130, 194);
}
input:focus + label {
right: 10px;
}
input[type="email"]:focus,
input[type="password"]:focus {
background-color: rgba(255, 255, 255, .8);
}
/* Submit */
input[type="submit"] {
background-color: #333;
background: -moz-linear-gradient(bottom, #333, #444);
background: -ms-linear-gradient(bottom, #333, #444);
background: -o-linear-gradient(bottom, #333, #444);
background: -webkit-linear-gradient(bottom, #333, #444);
background: linear-gradient(bottom, #333, #444);
border: 1px solid #222;
color: #fff;
cursor: pointer;
height: 35px;
width: 110px;
}Run Code Online (Sandbox Code Playgroud)
<form id="login">
<ul>
<li>
<input id="email" name="email" placeholder="Your Email" title="Your Email" type="email" required />
<label for="email">Your Email</label>
</li>
</ul>
</form>Run Code Online (Sandbox Code Playgroud)
Nic*_*o O 10
很难想象这种行为的好用例,因为它阻止了一些用户输入.
一种简单的方法是使用,input::after但现在任何浏览器都不支持(感谢@ JukkaK.Korpela).
但是您可以使用包装器元素和数据属性,如下所示:
<div class="placeholder" data-placeholder="my placeholder">
<input value="My text" />
</div>
Run Code Online (Sandbox Code Playgroud)
有了这个css:
.placeholder
{
position: relative;
}
.placeholder::after
{
position: absolute;
left: 5px;
top: 3px;
content: attr(data-placeholder);
pointer-events: none;
opacity: 0.6;
}
Run Code Online (Sandbox Code Playgroud)
导致: 
因为你必须做很多调整才能使它看起来很好,你也可以考虑使用包装<div>元素作为输入"外观相似":
<div class="editable" data-placeholder="my placeholder">
<input type="text" value="my Text" />
</div>
Run Code Online (Sandbox Code Playgroud)
CSS:
.editable
{
position: relative;
border: 1px solid gray;
padding: 3px;
background-color: white;
box-shadow: rgba(0,0,0,0.4) 2px 2px 2px inset;
}
.editable > input
{
position: relative;
z-index: 1;
border: none;
background-color: transparent;
box-shadow: none;
width: 100%;
}
.editable::after
{
position: absolute;
left: 4px;
top: 5px;
content: attr(data-placeholder);
pointer-events: none;
opacity: 0.5;
z-index: 1;
}
Run Code Online (Sandbox Code Playgroud)