placeholder的使用

1.定义

placeholder 属性提供可描述输入字段预期值的提示信息

该提示会在输入字段为空时显示,并会在字段获得焦点时消失。

注释:placeholder 属性适用于以下的 <input> 类型:text, search, url, telephone, email 以及 password。

2.用法

<input type="password" name="user_search" placeholder="Search W3School" />

3.缩进

::-webkit-input-placeholder {
    text-indent: 4px;
}
:-ms-input-placeholder {
    text-indent: 4px;
}
::-moz-placeholder {
    text-indent: 4px; 
    opacity: 1!important;
} 

input:focus::-webkit-input-placeholder {
  color: #999;
  border-color:#999;
}

4.低版本兼容

思路:

4.1 判断浏览器是否支持该元素

return 'placeholder' in document.createElement('input');

4.2 遍历文档所有带 placeholder 属性的 input

$(':input[placeholder]').each(function (index, element) {})

4.3  判断是否是 password 类型

return type.toLowerCase() === 'password';

4.4  针对每个匹配元素绑定事件 focus、blur、keydown...

总结:不同浏览器上展现效果不尽相同,模仿达到产品需求即可。



发现一个好玩的现象:
如果你把自己的表单放在一个白色背景下或者透明背景下,input设置为白色背景色和颜色,你会发现看起来聚焦不了了...


 

posted @ 2018-08-22 19:06  justSmile2  阅读(1289)  评论(0编辑  收藏  举报