栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

一个占位符中是否可能有多种文本颜色?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

一个占位符中是否可能有多种文本颜色?

不可以 ,无法为默认占位符着色,但是您可以创建类似于占位符的元素。因此,您可以为字母着色。这是默认占位符的解决方法。

请注意,我正在使用

opacity: 0.5
,您可以根据需要进行更改。


HTML

.input-field {    position: relative;    display: inline-block;}.input-field > label {    position: absolute;    left: 0.5em;    top: 50%;    margin-top: -0.5em;    opacity: 0.5;}.input-field > input[type=text]:focus + label {    display: none;}.input-field > label > span {    letter-spacing: -2px;}.first-letter {    color: red;}.second-letter {    color: blue;}.third-letter {    color: orange;}.fourth-letter {    color: green;}.fifth-letter {    color: yellow;}    <div >        <input id="input-text-field" type="text"></input>        <label for="input-text-field"> <span >H</span> <span >E</span> <span >L</span> <span >L</span> <span >O</span>        </label>    </div>

工作小提琴


更新:

仅CSS(
:placeholder-shown

上面的小提琴有一个Bug ,当您在文本框中键入内容并单击外部时,占位符将再次出现在输入的文本上方。

因此,要使其完美,我们可以使用

:placeholder-shown
除chrome和firefox之外没有太多支持的工具。

这是代码:

.input-field {  position: relative;  display: inline-block;}.input-field > label {  position: absolute;  left: 0.5em;  top: 50%;  margin-top: -0.5em;  opacity: 0.5;  display: none;}.input-field > input[type=text]:placeholder-shown + label {  display: block;}.input-field > label > span {  letter-spacing: -2px;}.first-letter {  color: red;}.second-letter {  color: blue;}.third-letter {  color: orange;}.fourth-letter {  color: green;}.fifth-letter {  color: yellow;}<div >  <input id="input-text-field" type="text" placeholder=" "></input>  <label for="input-text-field">    <span >H</span>    <span >E</span>    <span >L</span>    <span >L</span>    <span >O</span>  </label></div>

使用JS( 不带
:placeholder-shown
):

addListenerMulti(document.getElementById('input-text-field'), 'focus keyup', blurme);function blurme(e) {  var element = e.currentTarget;  element.classList[(element.value.length !== 0) ? "add" : "remove"]('hide-placeholder');}function addListenerMulti(el, s, fn) {  s.split(" ").forEach(function(e) {    return el.addEventListener(e, fn, false)  });}.input-field {  position: relative;  display: inline-block;}.input-field > label {  position: absolute;  left: 0.5em;  top: 50%;  margin-top: -0.5em;  opacity: 0.5;}.hide-placeholder + label {  display: none;}.input-field > label > span {  letter-spacing: -2px;}.first-letter {  color: red;}.second-letter {  color: blue;}.third-letter {  color: orange;}.fourth-letter {  color: green;}.fifth-letter {  color: yellow;}<div >  <input id="input-text-field" type="text"></input>  <label for="input-text-field">    <span >H</span>    <span >E</span>    <span >L</span>    <span >L</span>    <span >O</span>  </label></div>


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/432673.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号