【问题标题】:How can I highlight optional input:text if it isn't empty? [duplicate]如何突出显示可选输入:文本如果它不为空? [复制]
【发布时间】:2020-09-14 06:31:28
【问题描述】:

我找到了:required 输入的解决方案,但没有找到:optional 的解决方案。

感谢您的帮助,对不起我的英语,这不是我的母语。

【问题讨论】:

  • 你想在什么时候高亮输入?也许当用户将鼠标悬停在其上方或关注该字段时?

标签: javascript css forms


【解决方案1】:

注意:如果您的目标是现代浏览器并使用占位符(或者您可以使用占位符),您可以使用下面的Irina's :placeholder-shown 伪类而不是而不是 .empty) 并取消 JavaScript。它不适用于 IE 或 Edge v44 或更早版本,但适用于所有最新的现代浏览器。但是:对于 IE 和 Edge v44 及更早版本,您可以使用 -ms-input-placeholder,从而使以下 JavaScript 部分失效。但我不想编辑它以使用:placeholder-shown:-ms-input-placeholder,因为这重复了 Irina 的答案(有点),我不能删除这个答案,因为它被接受了。不过,请参阅答案末尾的示例。


我找到了 :required 输入的解决方案,但没有找到 :optional 的解决方案。

您可以通过两种方式做到这一点:

  1. 您可以使用:not(:required):notnegation pseudoclass。它接受simple selector 并反转其含义。 “不需要”是“可选”。

  2. 您可以以“可选”方式设置所有输入的样式,然后为 :required 元素覆盖该样式。

不幸的是,对于可选的 input 元素 (more here),您的问题的“空白”部分无法单独使用 CSS 来回答。

这是 #1 的示例,当输入为/不为空时,使用 JavaScript 添加/删除一个类:

"use strict";
function toggleEmptyClass(el) {
    if (el.tagName === "INPUT") {
        // If you need IE compatibility, you'll need an `if`/`else`;
        // IE doesn't support the second argument for `toggle`
        el.classList.toggle("empty", !el.value);
    }
}
document.querySelectorAll("input").forEach(toggleEmptyClass);
document.addEventListener("input", function() {
    toggleEmptyClass(event.target);
});
input:not(.empty):required {
    background-color: #FF8000;
}
input:not(.empty):not(:required) {
    background-color: #0080FF;
}
<div>
    <label>
        Required:
        <input type="text" required value="required">
    </label>
</div>
<div>
    <label>
        Required and (initially) empty:
        <input type="text" required>
    </label>
</div>
<div>
    <label>
        Optional:
        <input type="text" value="optional">
    </label>
</div>
<div>
    <label>
        Optional and (initially) empty:
        <input type="text">
    </label>
</div>

或者您可以只使用“可选”样式设置所有input 元素的样式,然后为:required 元素覆盖它:

例子:

"use strict";
function toggleEmptyClass(el) {
    if (el.tagName === "INPUT") {
        // If you need IE compatibility, you'll need an `if`/`else`;
        // IE doesn't support the second argument for `toggle`
        el.classList.toggle("empty", !el.value);
    }
}
document.querySelectorAll("input").forEach(toggleEmptyClass);
document.addEventListener("input", function() {
    toggleEmptyClass(event.target);
});
input:not(.empty) {
    background-color: #0080FF;
}
input:not(.empty):required {
    background-color: #FF8000;
}
<div>
    <label>
        Required:
        <input type="text" required value="required">
    </label>
</div>
<div>
    <label>
        Required and (initially) empty:
        <input type="text" required>
    </label>
</div>
<div>
    <label>
        Optional:
        <input type="text" value="optional">
    </label>
</div>
<div>
    <label>
        Optional and (initially) empty:
        <input type="text">
    </label>
</div>

这是一个完整的示例,展示了使用 :placeholder-shown 和较旧的 Microsoft 特定 -ms-input-placeholder 执行上述操作:

input:not(:placeholder-shown):required {
    background-color: #FF8000;
}
input:not(:placeholder-shown):not(:required) {
    background-color: #0080FF;
}
/* IE and Edge <= v44 support: */
input:not(:-ms-input-placeholder):required {
    background-color: #FF8000;
}
input:not(:-ms-input-placeholder):not(:required) {
    background-color: #0080FF;
}
<div>
    <label>
        Required:
        <input type="text" placeholder="placeholder" required value="required">
    </label>
</div>
<div>
    <label>
        Required and (initially) empty:
        <input type="text" placeholder="placeholder" required>
    </label>
</div>
<div>
    <label>
        Optional:
        <input type="text" placeholder="placeholder" value="optional">
    </label>
</div>
<div>
    <label>
        Optional and (initially) empty:
        <input type="text" placeholder="placeholder">
    </label>
</div>

或者,再次设置所有样式,然后仅覆盖 :required 以使其不同:

input:not(:placeholder-shown) {
    background-color: #0080FF;
}
input:not(:placeholder-shown):required {
    background-color: #FF8000;
}
/* IE and Edge <= v44 support: */
input:not(:-ms-input-placeholder) {
    background-color: #0080FF;
}
input:not(:-ms-input-placeholder):required {
    background-color: #FF8000;
}
<div>
    <label>
        Required:
        <input type="text" placeholder="placeholder" required value="required">
    </label>
</div>
<div>
    <label>
        Required and (initially) empty:
        <input type="text" placeholder="placeholder" required>
    </label>
</div>
<div>
    <label>
        Optional:
        <input type="text" placeholder="placeholder" value="optional">
    </label>
</div>
<div>
    <label>
        Optional and (initially) empty:
        <input type="text" placeholder="placeholder">
    </label>
</div>

【讨论】:

  • 不能使用这个方法。在这种情况下,样式将始终显示,但只有在输入不为空时才需要它们。
  • @IrinaZorg - 不幸的是,没有空/非空 input 元素的 CSS 选择器,只有有效/无效,这(我想你已经知道)对可选输入没有帮助.为此,您需要使用 JavaScript。 :-(
  • @IrinaZorg 你在找这个吗:jsfiddle.net/ogzw0a64/1 ?
  • @IrinaZorg - 我已经更新了答案以显示使用 JavaScript 切换类以在 CSS 中使用。
【解决方案2】:

找到了这个解决方案,使用:placeholder-shown

input:placeholder-shown {
  border: 1px solid #f5f5f5;
}

input {
  background: #ffffff;
  border: 10px solid;
  border-image-slice: 1;
  border-width: 1px;
  border-image-source: linear-gradient(
    171.2deg,
    #ffc1d4 15.78%,
    #ff866c 76.58%
  );
}
<form>
  <input type="text" name="name1" id="" placeholder="placeholder1" optional>
  <input type="text" name="name2" id="" placeholder="placeholder2" optional>
</form>

【讨论】:

  • 非常酷!!请注意:placeholder-shown 在任何版本的 IE 中都不起作用,并且在 Edge v44 或更早版本中不起作用(在大多数 Windows 安装中仍然是最新的——尽管不会持续很长时间,因为基于 Chromium 的 Edge v79 出现了1 月,并逐渐添加到自动更新中)。
  • 请注意,您仍然需要:not(:required) 或类似名称,以避免为所需元素设置样式。
  • 啊!但请参阅this answer 上的评论:有一个适用于 IE 的 Microsoft 特定版本::-ms-input-placeholder。因此,如果您同时使用两者(在单独但等效的规则中),则适用于 IE 和 Edge v44 及之前的版本。
【解决方案3】:

这是一个使用 javascript 和 css 的解决方案

CSS:

.highlighted
      {
        border: hsl(180, 60%, 60%) 2px solid;
      }

JS:

        let inputs = document.querySelectorAll('input:optional');
        for (let input of inputs)
        {
            input.addEventListener('change',function(e)
            {
              if (this.value != '')
              {
                this.classList.add('highlighted');
              }
              else
              {
                this.classList.remove('highlighted');
              }
            });
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-02-22
    • 1970-01-01
    • 1970-01-01
    • 2015-03-07
    • 2018-10-20
    • 2022-01-16
    • 1970-01-01
    相关资源
    最近更新 更多