【问题标题】:How to change placeholder color on focus?如何更改焦点上的占位符颜色?
【发布时间】:2012-10-22 09:57:35
【问题描述】:

如何在输入框获得焦点时改变占位符的颜色? 我使用这个 css 来设置默认颜色,但是如何在焦点上更改它?

::placeholder { color: blue; }

::-webkit-input-placeholder { color: blue; }

/* Firefox < 19 */
:-moz-placeholder { color: blue; }

/* Firefox > 19 */
::-moz-placeholder { color: blue; }

/* Internet Explorer 10 */
:-ms-input-placeholder { color: blue; }

【问题讨论】:

    标签: css input focus placeholder


    【解决方案1】:

    试试这个,应该可以的:

    input::-webkit-input-placeholder {
        color: #999;
    }
    input:focus::-webkit-input-placeholder {
        color: red;
    }
    
    /* Firefox < 19 */
    input:-moz-placeholder {
        color: #999;
    }
    input:focus:-moz-placeholder {
        color: red;
    }
    
    /* Firefox > 19 */
    input::-moz-placeholder {
        color: #999;
    }
    input:focus::-moz-placeholder {
        color: red;
    }
    
    /* Internet Explorer 10 */
    input:-ms-input-placeholder {
        color: #999;
    }
    input:focus:-ms-input-placeholder {
        color: red;
    }
    

    这是一个例子:http://jsfiddle.net/XDutj/27/

    【讨论】:

    • 为什么没有 :-ms-input-placeholder?
    • 我没有IE来测试上面的,如果有人能测试一下就好了。 AFAIK,IE9 缺乏占位符支持:caniuse.com/#search=placeholder
    • 对我来说就像一个魅力。不过显然没有在 IE 中测试。
    • 不,遗憾的是它在 IE 10 中不起作用,因为 IE 使用伪类 :-ms-input-placeholder 而不是伪元素 ::-ms-placeholder。无法测试焦点行为,因为默认情况下 IE 将占位符隐藏在焦点上(不幸的是我没有找到避免这种情况的方法)。 See this update to your Fiddle.
    【解决方案2】:

    这对我有用:

    input:focus::placeholder {
      color: blue;
    }
    

    【讨论】:

    • 这应该改为标记答案。
    【解决方案3】:

    除了 Pranav 的回答,我还改进了与 textarea 兼容的代码:

    ::-webkit-input-placeholder { color: #999; }
    :-moz-placeholder { color: #999; }
    
    :focus::-webkit-input-placeholder { color: #ccc; }
    :focus:-moz-placeholder { color: #ccc; }​
    

    【讨论】:

      【解决方案4】:

      以下内容对我有用:

      input:focus::-webkit-input-placeholder
      {
          color: red;
      }
      

      【讨论】:

        【解决方案5】:

        试试这个:

        HTML

        <input type='text' placeholder='Enter text' />
        

        CSS

        input[placeholder]:focus { color: red; }
        

        【讨论】:

        • 这不是必需的,但我怀疑这是因为在进一步检查时,您的代码会将输入的实际值着色为焦点,而不仅仅是其占位符文本。
        【解决方案6】:

        我用 JQuery 找到了这个解决方案:

         $('input[type="text"]').each(function(){
        
            $(this).focus(function(){
              $(this).addClass('input-focus');
            });
        
            $(this).blur(function(){
              $(this).removeClass('input-focus');
            });
        
          });
        

        使用这个 css:

        .input-focus::-webkit-input-placeholder { color: #f00; }    
        .input-focus:-moz-placeholder { color: #f00; }
        .input-focus:-ms-input-placeholder { color: #f00; }
        

        【讨论】:

        • JQuery 很漂亮,但必须死。一切有始有终,@DavidePalmieri
        • 你在@BoltClock上。当然它需要 jQuery...如果有什么它需要 moar jQuery!
        【解决方案7】:

        使用星号* 选择所有内容

        *::-webkit-input-placeholder { color: #999; }
        
        
        *:-moz-placeholder { color: #999; }
        
        
        *::-moz-placeholder { color: #999; }
        
        
        *:-ms-input-placeholder { color: #999; }
        

        【讨论】:

        • 注意:由于某种原因,您不能使用逗号来应用多个规则。如图所示,这些必须单独声明。 (尽管您现在可能只需要 ms-input 和 ::placeholder)
        【解决方案8】:

        完整和更新 (2021) 示例:

        input::placeholder {
          color: blue;
        }
        
        input:focus::placeholder {
          color: green;
        }
        <label for="city">City:</label><br>
        <input type="text" id="city" name="city" placeholder="your favorite city">

        【讨论】:

          【解决方案9】:

          从 Firefox 19 开始: 将表单元素与 placeholder 属性匹配的 :-moz-placeholder 伪类已被移除,而添加了 ::-moz-placeholder 伪元素。

          input:focus::-moz-placeholder { color: transparent; }
          

          【讨论】:

            【解决方案10】:

            您可以创建一个材料设计动画占位符,当输入字段被聚焦时,它会在顶部收缩。

            <div class="field">
             <input type="text" name="user" required><br>
             <label>Enter Username</label>
             </div>
            

            基本上,标签字段将充当占位符。我们只能使用 css 来做到这一点。 这里解释http://www.voidtricks.com/create-material-design-animated-placeholder/

            【讨论】:

              猜你喜欢
              • 2018-10-01
              • 1970-01-01
              • 2016-01-22
              • 2019-10-09
              • 2012-04-12
              • 1970-01-01
              • 2012-09-14
              • 2012-08-03
              • 2012-04-01
              相关资源
              最近更新 更多