【问题标题】:How can I do that the input will not get style here?我该怎么做才能让输入在这里没有样式?
【发布时间】:2020-12-19 02:12:59
【问题描述】:

问题。我希望这里的输入元素在他为空并且不从范围内选择器中获取样式时没有任何样式,就像他是“范围内”一样...

我该怎么做?我需要做的是输入不会在这里得到样式?代码如下:

li {
    list-style: none;
    margin-bottom: 1em;
  }
 
  input {
    border: 1px solid black;
 }
 
  input:in-range {
    background-color: rgba(251, 255, 0, 0.815);
  }
 
  input:out-of-range {
    background-color: rgba(255, 0, 0, 0.25);
    border: 2px solid red;
  }
 
  input:in-range + label::after {
    content: 'okay.';
  }
 
  input:out-of-range + label::after {
    content: 'out of range!';
  }
<!DOCTYPE html>
<html>  
 
<head>
<meta charset="UTF8">
<title>Practice vsc</title>
<link rel="stylesheet" href="css/style.css">
</head>
 
 
<body>
 
  <ul>Values between 1 and 10 are valid.
    <li>
      <input id="value1" name="value1" type="number" placeholder="1 to 10" min="1" max="10" value="12">
      <label for="value1">Your value is </label>
    </li>
  </ul>  
 
</body>
 
</html>

【问题讨论】:

  • 你会用javascript吗?
  • 我对 JS 不太了解,但如果你能给出代码,它会帮助兄弟

标签: html css


【解决方案1】:

li {
    list-style: none;
    margin-bottom: 1em;
  }
 
  input {
    border: 1px solid black;
 }
 
  input:in-range {
    background-color: rgba(251, 255, 0, 0.815);
  }
 
  input:out-of-range {
    background-color: rgba(255, 0, 0, 0.25);
    border: 2px solid red;
  }

  input:placeholder-shown {
    background-color: white;
  }
 
  input:in-range + label::after {
    content: 'okay.';
  }
 
  input:out-of-range + label::after {
    content: 'out of range!';
  }
<!DOCTYPE html>
<html>  
 
<head>
<meta charset="UTF8">
<title>Practice vsc</title>
<link rel="stylesheet" href="css/style.css">
</head>
 
 
<body>
 
  <ul>Values between 1 and 10 are valid.
    <li>
      <input id="value1" name="value1" type="number" placeholder="1 to 10" min="1" max="10" value="12">
      <label for="value1">Your value is </label>
    </li>
  </ul>  
 
</body>
 
</html>

这使用:placeholder-shown 选择器,如here 所示。

【讨论】:

    【解决方案2】:

    这是使用 JS 的方法,但更好的答案是占位符显示

    function check(){
    
      var input = document.getElementById('value1');
      
      if (input.value ==''){
        input.classList.add("nob"); 
      }else{
        input.classList.remove("nob"); 
      }
    }
    li {
        list-style: none;
        margin-bottom: 1em;
      }
     
      input {
        border: 1px solid black;
     }
     
      input:in-range {
        background-color: rgba(251, 255, 0, 0.815);
      }
     
      input:out-of-range {
        background-color: rgba(255, 0, 0, 0.25);
        border: 2px solid red;
      }
     
      input:in-range + label::after {
        content: 'okay.';
      }
     
      input:out-of-range + label::after {
        content: 'out of range!';
      }
      
      input.nob{
        background-color: white;
      }
      
    <!DOCTYPE html>
    <html>  
     
    <head>
    <meta charset="UTF8">
    <title>Practice vsc</title>
    <link rel="stylesheet" href="css/style.css">
    </head>
     
     
    <body>
     
      <ul>Values between 1 and 10 are valid.
        <li>
          <input id="value1" class='nob' onkeyup="check()" name="value1" type="number" placeholder="1 to 10" min="1" max="10" >
          <label for="value1">Your value is </label>
        </li>
      </ul>  
     
    </body>
     
    </html>

    【讨论】:

    • 谢谢你们,我会试试的
    猜你喜欢
    • 1970-01-01
    • 2014-03-05
    • 2019-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多