【问题标题】:Show placeholder until user inputs text without spaces显示占位符,直到用户输入没有空格的文本
【发布时间】:2017-07-21 17:50:11
【问题描述】:

考虑下面的 HTML 代码。当用户开始输入任何字符(包括空格)时,占位符会立即消失。

<input type="text" id="name"  placeholder="Enter Your Name"/>

有没有办法让占位符可见,直到用户输入没有空格的文本?

【问题讨论】:

  • 当用户输入时,你想在哪里显示占位符?那不是很古怪吗?
  • 否 - 占位符文本与输入到文本输入中的内容互斥。唯一的方法——我不推荐它——是有一个 javascript 函数将文本设置为值,直到你想用你设置的任何值替换该值。但这是一个坏主意。当占位符文本被保留为文本的值时,即使在用户输入其他内容之后,也会造成混淆。不要这样做。
  • @KarthikChintala - 仅适用于屏幕上不可见的空间。
  • 一种解决方案是使用单独的 DOMElement 伪造占位符,然后检查输入,如果它是有效输入,则可以将 DOMElement 移动为“真实”标签。 :) 我在一些网站上看到过,看起来还不错。

标签: javascript html input


【解决方案1】:

当键是空格时,您可以处理 keydown 事件来停止它。

document.getElementById("name").addEventListener("keydown", function(e) {
  if(document.getElementById("name").value.length === 0 && e.key === ' ') {
    if(e.preventDefault) { e.preventDefault(); }
    else { e.returnValue = false; }
  }
});
<input type="text" id="name"  placeholder="Enter Your Name"/>

【讨论】:

  • 尽可能简单,为这个干净的简单解决方案点赞
  • 感谢您接受 anwser。很高兴看到我可以帮助你
【解决方案2】:

遵循 CSS 脚本和 html 代码即可。

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

【讨论】:

  • 这很有帮助,但我可能需要稍微调整一下。谢谢。
  • 是的,这就是学习的方式。根据您的情况调整任何内容。 :)
【解决方案3】:

占位符仅在元素中没有输入的情况下可见。

如果您想强制此占位符在出现任何非空白字符之前可见,您可以过滤掉输入数据并拒绝任何空白。

【讨论】:

  • 我猜filter out the input data and reject any whitespaces你指的是Béranger给出的解决方案
【解决方案4】:

/**
 * Since inputs are replace elements(1), using 
 * pseudo-elements like ::after will not work.
 * Thus, the data-placeholder on the labels.
 *
 * (1) http://reference.sitepoint.com/css/replacedelements
 */ 

// Select all text input fields
var formText = document.querySelectorAll('[data-js=form-text]');

// For each field...
[].forEach.call( formText, function(el, i) {
  // Cache the field label
  var thisLabel = el.parentNode.querySelector('label');
  
  // Add an 'active' class if text present
  el.onkeyup = function () {
  
    if (el.value.length > 0) {
      thisLabel.classList.add('active');
    } else {
      thisLabel.classList.remove('active');
    }
  
  };
  
}); 
body{
  margin: 2em;
}

label {
  display: inline-block;    
  position: relative;
  width: 100%;
}

label.active:after {
  bottom: -2.8em;
  color: #888;
  content: attr(data-placeholder);
  left: 0;
  position: absolute;
}

input {
  margin: 0 0 2em 0;
}
<form>  
  
  <div>
    <label for="name" data-placeholder="First and Last">Name:</label>
    <input type="text" id="name" placeholder="First and Last" autocomplete="off" data-js="form-text">
  </div>
      
  <div>
    <label for="Phone" data-placeholder="###-###-####">Phone:</label>
    <input type="tel" id="phone" placeholder="###-###-####" autocomplete="off" data-js="form-text">
  </div>
    
  <div>
    <label for="email" data-placeholder="user@domain.com">Email:</label>
    <input type="email" id="email" placeholder="user@domain.com" autocomplete="off" data-js="form-text">
  </div>
    
</form> 

【讨论】:

  • 这完全改变了占位符文本的位置。不过,我非常感谢您的帮助。
猜你喜欢
  • 1970-01-01
  • 2023-02-15
  • 2023-03-20
  • 2020-03-06
  • 2011-03-27
  • 2013-12-28
  • 2022-01-21
  • 2017-05-11
  • 2021-01-17
相关资源
最近更新 更多