【问题标题】:HTML - Keep placeholder when user typesHTML - 用户键入时保留占位符
【发布时间】:2014-12-07 02:08:13
【问题描述】:

我有这样的输入:

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

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

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

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

【问题讨论】:

  • 我认为这不是一个好习惯。您可以使用将保留在那里的 。或者只是在用户开始输入时以编程方式显示输入标题
  • @Meer 你能举个例子吗?喜欢小提琴吗?
  • @Tambo 你能举个例子吗?喜欢小提琴吗?
  • 这个问题可以使用背景图片。
  • @ShirinAbdolahi 是的,但我也希望能够使用 JavaScript 更改背景文本

标签: javascript html css


【解决方案1】:

很难为这种行为想出一个好的用例,因为它会阻止一些用户输入。

一个简单的方法是使用input::after,但目前任何浏览器都不支持(感谢@JukkaK.Korpela)。

但是你可以使用包装元素和数据属性,如下:

<div class="placeholder" data-placeholder="my placeholder">
    <input value="My text" />  
</div>

有了这个css:

.placeholder
{
    position: relative;
}

.placeholder::after
{
    position: absolute;
    left: 5px;
    top: 3px;
    content: attr(data-placeholder);
    pointer-events: none;
    opacity: 0.6;
}

导致:

Click here for jsFiddle demo.


由于您必须进行大量调整才能使其看起来不错,因此您也可以考虑使用包装 &lt;div&gt; 元素作为“看起来相似”的输入:

<div class="editable" data-placeholder="my placeholder">
    <input type="text" value="my Text" />
</div>

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

Click here for the Demo 3. (with mocked &lt;input /&gt;)

Click here for the Demo 2. (with contenteditable)

【讨论】:

  • 很好,但对我来说,“我的占位符”文本在“我的文本 awd”下有点小,它发生在 Chrome 和 Firefox 中,无论如何要解决这个问题?
  • 不是你想要的方式。您可以为&lt;input&gt; 制作透明背景并使用z-index 更改堆叠,但这变得非常难看:jsfiddle.net/dkoruadc/1。您也可能会在使用不同的浏览器和操作系统时遇到问题。
  • 用户写的文字也会变得不那么可读。你为什么首先尝试这个?
  • @Meer 你是在向 OP 讲话还是向我讲话?我正在尝试这个来帮助和展示可能性。我并没有说在生产中使用它是个好主意。
  • input::after 只是当前浏览器不支持;没有任何无效的内容(CSS 伪元素不是 HTML 标记的一部分,HTML 规则不适用于它们)。
【解决方案2】:

通过 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;
}
<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>

【讨论】:

  • 但这不是我想要达到的目标。
  • @super 是的,但我无法想象你为什么要保留占位符。这是糟糕的设计,看起来非常丑陋。
【解决方案3】:

这可以通过使用“onchange”处理程序来完成。您将编写一个奇特的函数,将占位符的其余部分连接到用户键入的内容上,并将光标放在用户文本的末尾。

这里有一些未经测试的、不完整的 js/psuedocode 给你一个想法:

userTextLength: 0, // measure of how many chars the user has typed; need this because the length itself won't be a valid measure, since we're modifying it in place. Note that we're using the DOM as a source of truth here... alternative method would be to store the user's text itself here, but let's run with this.
placeholder: "xx/yy/zz",
onchange: function() {
  boxText = document.querySelector('#elem').value;
  if (boxText.length === 1) { // special handling for the first character they type. (Using placeholder text at first.)
    this.userTextLength++;
    placeholder = boxText.slice(userTextLength);
    userText = boxText.slice(0, userTextLength);
    document.querySelector('#elem').innerHTML = userText + placeholder;
  }
  if (boxText.length < placeholder.length) { // this would mean they used backspace, which also needs to be handled.

  }
  else { // the normal case, should look quite similar to the first if block
    this.userTextLength += 1;
    userInput = 
  }
}

我在这里没有处理的是光标聚焦。这将需要一个“onfocus”事件,并且还将使用 userTextLength 属性来决定放置它的位置。对于这方面的一些帮助,this answer 看起来应该会有所帮助。

【讨论】:

    【解决方案4】:

    这是不可能的,如果是的话,那将很没有吸引力。但我有一个想法可以帮助你支持 jquery。

    您可以在此处查看演示:http://hangaumy.com/order/

    当你输入时,它会自动在里面添加单词(看起来像占位符)

    【讨论】:

      【解决方案5】:

      你可以尝试做这样的事情:

      HTML:

      <div class="wrapper">
        <input type="text">
        <span class="placeholder">Placeholder</span>
      </div>
      

      CSS:

      .wrapper{
        position: relative;
      }
      
      input {
        font-size: 14px;
        height: 40px;
      }
      
      .placeholder {
        position: absolute;
        font-size:25px;
        pointer-events: none;
        left: 1px;
        top: 1px;
        transition: 0.1s ease all;
      }
      
      input:focus ~ .placeholder{
        top: 1px;
        font-size: 11px;
      }
      

      JSFiddle

      【讨论】:

      • 这是迄今为止最优雅的解决方案。我会做噩梦,用同时显示的 2 组字符填充输入框(根据以前的答案),但你救了我
      • JSFiddle 仍然在用户输入的文本之上显示占位符文本 - 这是它应该如何工作还是这个解决方案随着时间的推移而中断?只是检查一下,因为我喜欢这个想法并想尝试一下。
      【解决方案6】:

      .box {
        border: 1px solid;
        border-radius: 10px;
        padding: .25rem 1rem 1rem;
        color: #555;
        font-family: sans-serif;
        display: flex;
        flex-direction: column;
        width: max-content;
      }
      
      .wrapper {
        position: relative;
        width: 450px;
      }
      
      .wrapper * {
        font-size: 1.25rem;
        letter-spacing: 2px;
        font-family: monospace;
        padding: .125rem .25rem;
        display: flex;
        width: calc(100% - 1rem);
      }
      
      input {
        width: 4000px;
        border: 0;
      }
      
      .placeholder {
        position: absolute;
        pointer-events: none;
        left: 0;
        top: 0;
        width: min-content;
      }
      <div class="box">
        <h2>Short Homepage Headline</h2>
        <p>Use up tp 30 characters</p>
        <div class="wrapper">
          <input type="text">
          <span class="placeholder">
            ______________________________
          </span>
        </div>
      </div>

      这对于功能、良好的用例及其吸引力如何?
      (试图消除上述一些负面影响,哈哈)

      1. 占位符文本是有限数量的下划线 (30)?
      2. 相同的字体大小、等宽和字母间距

      这为标题作者提供了一个简洁的无 js 字符观察器。这样,他们将能够看到它何时会破坏模板。但是,您不一定必须将其绑定到硬性限制。

      【讨论】:

        猜你喜欢
        • 2018-03-06
        • 2013-10-31
        • 2020-12-09
        • 1970-01-01
        • 2023-04-09
        • 2014-08-22
        • 2013-05-02
        • 1970-01-01
        相关资源
        最近更新 更多