zoukankan      html  css  js  c++  java
  • 使用CSS修改HTML5 input placeholder颜色( 转载 )

    问题:Chrome支持input=[type=text]占位文本属性,但下列CSS样式却不起作用:

    input[placeholder], [placeholder], *[placeholder] {
       color:red !important;
    }

    HTML代码:

    <input type="text" placeholder="Value" />

    运行结果值还是灰色,Color:red没有作用。有什么方法可以修改占位文本的颜色吗?我在浏览器里安装了jQuery占位文本插件,但仍然无用。

    回答:

    有三种实现方式:伪元素(pseudo-elements)、伪类( pseudo-classes)和Notihing。

    WebKit和Blink(Safari,Google Chrome, Opera15+)使用伪元素。

    ::-webkit-input-placeholder

    Mozilla Firefox 4-18使用伪类:

    :-moz-placeholder

    Mozilla Firefox 19+ 使用伪元素:

    ::-moz-placeholder

    IE10使用伪类:

    :-ms-input-placeholder

    CSS选择器

    因为每个浏览器的CSS选择器都有所差异,所以需要针对每个浏览器做单独的设定。

    ::-webkit-input-placeholder { /* WebKit browsers */
        color:    #999;
    }
    :-moz-placeholder { /* Mozilla Firefox 4 to 18 */
        color:    #999;
    }
    ::-moz-placeholder { /* Mozilla Firefox 19+ */
        color:    #999;
    }
    :-ms-input-placeholder { /* Internet Explorer 10+ */
        color:    #999;
    }

    textareas(文本框可拉伸)风格样式的代码,如下:

    input::-webkit-input-placeholder, textarea::-webkit-input-placeholder {
      color: #636363;
    }
    input:-moz-placeholder, textarea:-moz-placeholder {
      color: #636363;
    }

    input和Textarea的字体颜色均为红色。所有样式都要针对不同的选择器而定,不要打包整体处理,因为其中一个出问题,其他的都会失效。

    *::-webkit-input-placeholder {
        color: red;
    }
     
    *:-moz-placeholder {
        color: red;
    }
     
    *:-ms-input-placeholder {
        /* IE10+ */
        color: red;
    }

    在Firefox和IE里,正常input文本颜色覆盖占位符颜色的方法:

    ::-webkit-input-placeholder { 
        color: red; text-overflow: ellipsis; 
    }
    :-moz-placeholder { 
        color: #acacac !important; text-overflow: ellipsis; 
    }
    ::-moz-placeholder { 
        color: #acacac !important; text-overflow: ellipsis; 
    } /* for the future */
    :-ms-input-placeholder { 
        color: #acacac !important; text-overflow: ellipsis; 
    }

    还有一种好办法:

    input::-webkit-input-placeholder, textarea::-webkit-input-placeholder { 
        color:    #666;
    }
    input:-moz-placeholder, textarea:-moz-placeholder { 
        color:    #666;
    }
    input::-moz-placeholder, textarea::-moz-placeholder { 
        color:    #666;
    }
    input:-ms-input-placeholder, textarea:-ms-input-placeholder { 
        color:    #666;
    }

    最后一种是从网上找的:

    $('[placeholder]').focus(function() {
            var input = $(this);
            if (input.val() == input.attr('placeholder')) {
                input.val('');
                input.removeClass('placeholder');
            }
        }).blur(function() {
            var input = $(this);
            if (input.val() == '' || input.val() == input.attr('placeholder')) {
                input.addClass('placeholder');
                input.val(input.attr('placeholder'));
            }
        }).blur();
        $('[placeholder]').parents('form').submit(function() {
            $(this).find('[placeholder]').each(function() {
                var input = $(this);
                if (input.val() == input.attr('placeholder')) {
                    input.val('');
                }
            })
        });

    这个代码调用的规则是,先加载Javascript再用CSS修改占位符属性。

    form .placeholder {
       color: #222;
       font-size: 25px;
       /* etc */
    }

    还有一种,不用CSS和占位文本,同样能得到相同效果。

    <input type="text" value="placeholder text" onfocus="this.style.color='#000'; this.value='';" style="color: #f00;"/>

    文章来源:http://html5.9tech.cn/news/2013/1118/38691.html

  • 相关阅读:
    java.lang.IllegalStateException: Cannot get a STRING value from a NUMERIC cell
    spring boot 启动报:Composite-id class must implement Serializable: xxx错误
    IntelliJ IDEA 中使用region代码折叠
    JSON parse error: Cannot deserialize value of type `java.time.LocalDateTime` from String
    MySQL生成UUID
    PowerDesigner设计表时显示注释列Comment,Columns中没有Comment的解决办法
    使用PowerDesigner创建物理模型并生成SQL语句在MySQL中生成表
    IMP-00003: 遇到 ORACLE 错误 959 ORA-00959: 表空间 '' 不存在
    SP2-0734: 未知的命令开头 “IMP ”
    使用imp命令和exp命令对oracle数据库进行导入导出操作
  • 原文地址:https://www.cnblogs.com/codinganytime/p/5341294.html
Copyright © 2011-2022 走看看