javascript - HTML - 在用户键入时保留占位符

标签 javascript html css

我有这样的输入:

<input value="My text" placeholder="Placeholder">

当我在输入中键入内容时,占位符文本将消失,这是很明显的。

现在,我想要做的是,我希望占位符文本在用户键入时保留,以便您可以将占位符文本视为原始文本后面的背景文本:

placeholder

编辑:我还希望能够使用 JavaScript 更改背景文本。

最佳答案

更好的解决方案,通过 CSS 实现轻松效果。看看:http://jsfiddle.net/csdtesting/wbqq129q/

  • 打字前:

enter image description here

  • 打字时:

enter image description here

代码:

#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;
}
<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>

关于javascript - HTML - 在用户键入时保留占位符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26324252/

相关文章:

javascript - 类型错误 : a is undefined Highmaps

javascript - React router dom 库不适用于我的项目

javascript - CSS或JS修改(替换)网页上的每一个字

javascript - 创建一个勾选框,然后通过单击自动勾选所有其他勾选框,并显示一个按钮

c# - 按钮单击事件后的 Chrome 滚动条不显示?

html - 以百分比设置div的高度

javascript - angularJS - 按键过滤 JSON

html - 删除导航下拉列表中的分词符

html - 目标第一个 Div 图像 CSS

html - Bootstrap : how to add vertical offset in columns?