• HTML5 input placeholder 颜色修改示例


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

    CSS 

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

    HTML input语句 

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

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

    回答: 

    toscho:有三种实现方式:伪元素(pseudo-elements)、伪类( pseudo-classes)和Notihing。 
    WebKit和Blink(Safari,Google Chrome, Opera15+)使用伪元素 

    ::-webkit-input-placeholder 

    Mozilla Firefox 4-18使用伪类 

    Mozilla Firefox 4-18使用伪类 

    Mozilla Firefox 19+ 使用伪元素 

    ::-moz-placeholder 

    IE10使用伪类 

    :-ms-input-placeholder 

    IE9和Opera12以下版本的CSS选择器均不支持占位文本。需要注意的是伪元素在Shadow DOM里会起到元素的真实作用。 

    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; 
    } 

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

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

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

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

    James Donnelly:在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 */ 
    } 

    user1729061:不用CSS和占位文本,同样能得到相同效果。 

    <input type="text" value="placeholder text" onfocus="this.style.color='#000';this.value='';" style="color: #f00;"/> 
  • 相关阅读:
    禁止用户打开HTML页面调试
    Git 忽略提交 .gitignore
    PHP日期时间处理
    HTML页面中JavaScript能获取到的各种屏幕大小信息
    移动浏览器H5页面通过scheme打开本地应用
    Python爬虫利器:Beautiful Soup
    MySQL分页查询优化
    JavaScrpit中异步请求Ajax实现
    PHP反射机制
    CentOS下nginx php mysql 环境搭建
  • 原文地址:https://www.cnblogs.com/shenyixin/p/5742316.html
Copyright © 2020-2023  润新知