如何选择伪元素(after)以在无效和有效输入中更改样式。

huangapple go评论70阅读模式
英文:

how to select a psoudo element (after) for change style in invalid and valid input

问题

I add * after an input,
I want to select this to change the color in input:valid and input:invalid,
but this CSS is not working. Why?

form::after {
    content: "*";
}

#fname:invalid ~::after {
    background: red;
    border-color: yellow;
}

#fname:valid ~::after {
    background: rgb(3, 183, 168);
    border-color: rgb(9, 9, 0);
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <form action="/action_page.php" id="frm">
        <label for="fname">First name:</label><br>
        <input required type="text" id="fname" value=""><br><br>
        <input type="submit" value="Submit">
    </form>
</body>
</html>
英文:

i add * after a input,
i want select this for change color in input:valid and invalid,
but by this CSS not working why?

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

form::after {
    content: &quot;*&quot;;
}

#fname:invalid~::after {
    background: red;
    border-color: yellow;
}

#fname:valid~::after {
    background: rgb(3, 183, 168);
    border-color: rgb(9, 9, 0);
}

<!-- language: lang-html -->

&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;
&lt;head&gt;
    &lt;meta charset=&quot;UTF-8&quot;&gt;
    &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
    &lt;title&gt;Document&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
     &lt;form action=&quot;/action_page.php&quot; id=&quot;frm&quot;&gt;
        &lt;label for=&quot;fname&quot;&gt;First name:&lt;/label&gt;&lt;br&gt;
        &lt;input required type=&quot;text&quot; id=&quot;fname&quot; value=&quot;&quot;&gt;&lt;br&gt;&lt;br&gt;
        &lt;input type=&quot;submit&quot; value=&quot;Submit&quot;&gt;
    &lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;

<!-- end snippet -->

答案1

得分: 1

你可以使用:has()(在 Firefox 中需要启用 layout.css.has-selector.enabled 标志):

#frm:after {
  content: "*";
}

#frm:has(#femail:invalid):after {
  background: red;
}

#frm:has(#femail:valid):after {
  background: rgb(3, 183, 168);
}

或者简单地将<form>中的 :after 替换为,例如,<span>*</span>

#femail:invalid ~ span {
  background: red;
}

#femail:valid ~ span {
  background: rgb(3, 183, 168);
}
<form action="?" id="frm">
  <label for="fname">Email:</label><br>
  <input required type="email" id="femail"><br><br>
  <input type="submit" value="Submit">
  <span>*</span>
</form>
英文:

You can use :has() (supported in Firefox behind the layout.css.has-selector.enabled flag):

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

#frm:after {
  content: &quot;*&quot;;
}

#frm:has(#femail:invalid):after {
  background: red;
}

#frm:has(#femail:valid):after {
  background: rgb(3, 183, 168);
}

<!-- language: lang-html -->

&lt;form action=&quot;?&quot; id=&quot;frm&quot;&gt;
  &lt;label for=&quot;fname&quot;&gt;Email:&lt;/label&gt;&lt;br&gt;
  &lt;input required type=&quot;email&quot; id=&quot;femail&quot;&gt;&lt;br&gt;&lt;br&gt;
  &lt;input type=&quot;submit&quot; value=&quot;Submit&quot;&gt;
&lt;/form&gt;

<!-- end snippet -->

Or simply replace :after in &lt;form&gt; with, say, &lt;span&gt;*&lt;/span&gt;:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

#femail:invalid ~ span {
  background: red;
}

#femail:valid ~ span {
  background: rgb(3, 183, 168);
}

<!-- language: lang-html -->

&lt;form action=&quot;?&quot; id=&quot;frm&quot;&gt;
  &lt;label for=&quot;fname&quot;&gt;Email:&lt;/label&gt;&lt;br&gt;
  &lt;input required type=&quot;email&quot; id=&quot;femail&quot;&gt;&lt;br&gt;&lt;br&gt;
  &lt;input type=&quot;submit&quot; value=&quot;Submit&quot;&gt;
  &lt;span&gt;*&lt;/span&gt;
&lt;/form&gt;

<!-- end snippet -->

答案2

得分: 1

input:valid + span::after {
  content: &quot;&quot;;
  background: red;
}
英文:

Replace ::after with span

&lt;form action=&quot;/action_page.php&quot; id=&quot;frm&quot;&gt;
    &lt;label for=&quot;fname&quot;&gt;First name:&lt;/label&gt;&lt;br&gt;
    &lt;input required type=&quot;text&quot; id=&quot;fname&quot; value=&quot;&quot;&gt;&lt;br&gt;&lt;br&gt;
    &lt;span&gt;span&lt;/span&gt;
    &lt;input type=&quot;submit&quot; value=&quot;Submit&quot;&gt;
&lt;/form&gt;
input:valid + span::after {
  content: &quot;&quot;;
  background: red;
}

huangapple
  • 本文由 发表于 2023年6月25日 21:12:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76550575.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定