**昨天后台小哥哥提到placehold无法显示问题,我这边总结一下,顺便写个小文章分享给大家。。**

==============================================

一、解决ie9以下input 无placeholder问题

解决方案一:jquery实现
  • 当浏览器不支持placeholder属性时,克隆一个和界面相同的input框,将placeholder的值设置为其value值,覆盖在界面input框所在位置,并将界面上的input隐藏掉
    调用方法:

     $(#selector).placeholder();(selector泛指css 的 id选择器)
  • 当文本框type=password时,引用此placeholder方案会使暗文密码变成明文密码
  • 如果input文本框使用了bootstrap 行高会高一点,要修改placeholder内的文字样式 可在placeholder.js里<span></span>中添加style属性如:


    <span style="font-size: 13px;padding-top: 8px;"></span>

    如果是普通的input 则无需添加style属性,


提取demo 链接:https://pan.baidu.com/s/1AMl6... 密码:zs9c

解决方案二: js/jQuery实现
  • 实现思路:
    1、首先判断浏览器是否支持placeholder属性,如果不支持则使用模拟placeholder
    2、创建一个label标签:<label>密码</label>
    标签里面的内容为所要添加的提示文字,该文字应该从对应的input|textarea标签取得其placeholder属性值,再将label标签遮盖
    到所对应的input|textarea上
    3、代码实现如下:
  (function (win) {

    win.isPlaceholer = function () {
var input = document.createElement("input");
return "placeholder" in input;
}; win.placeholder = function () { if (!isPlaceholer()) {
var Placeholder =function (obj) {
this.input = obj;
var te = obj.getAttribute('placeholder');
this.label = document.createElement('label');
this.label.innerHTML = te;
this.label.id = obj.id + 'Label';
this.label.style.cssText = 'position:absolute; text-indent:4px;color:#999999; font-size:14px;';
if (obj.value !== '') {
this.label.style.display = 'none';
}
this.init();
};
Placeholder.prototype = { getxy: function (obj) {
var left, top;
if (document.documentElement.getBoundingClientRect) {
var html = document.documentElement,
body = document.body,
pos = obj.getBoundingClientRect(),
st = html.scrollTop || body.scrollTop,
sl = html.scrollLeft || body.scrollLeft,
ct = html.clientTop || body.clientTop,
cl = html.clientLeft || body.clientLeft;
left = pos.left + sl - cl;
top = pos.top + st - ct;
} else {
while (obj) {
left += obj.offsetLeft;
top += obj.offsetTop;
obj = obj.offsetParent;
}
}
return {
left: left,
top: top
};
}, getwh: function (obj) {
return {
w: obj.offsetWidth,
h: obj.offsetHeight
};
}, setStyles: function (obj, styles) {
for (var p in styles) {
obj.style[p] = styles[p] + 'px';
}
},
init: function () {
var label = this.label,
input = this.input,
getXY = this.getxy,
xy = this.getxy(input),
wh = this.getwh(input);
this.setStyles(label, { 'width': wh.w, 'height': wh.h, 'lineHeight': 40, 'left': xy.left + 8, 'top': xy.top });
document.body.appendChild(label);
label.onclick = function () {
this.style.display = "none";
input.focus();
};
input.onfocus = function () {
label.style.display = "none";
};
input.onblur = function () {
if (this.value === "") {
label.style.display = "block";
}
};
if (window.attachEvent) {
window.attachEvent("onresize", function () {
var xy = getXY(input);
Placeholder.prototype.setStyles(label, { 'left': xy.left + 8, 'top': xy.top });
});
} else {
window.addEventListener("resize", function () {
var xy = getXY(input);
Placeholder.prototype.setStyles(label, { 'left': xy.left + 8, 'top': xy.top });
}, false);
}
}
}; var inpColl = $("#Box input:visible");//这里是页面上要添加placeholder支持的input
//var inpColl = document.getElementsByTagName('input'),
var textColl = document.getElementsByTagName('textarea');//这里是页面上要添加placeholder支持的textarea
//var lableArr = $("#Box lable");
var toArray = function (coll) {
for (var i = 0, a = [], len = coll.length; i < len; i++) {
a[i] = coll[i];
}
return a;
};
var inpArr = toArray(inpColl),
textArr = toArray(textColl), placeholderArr = inpArr.concat(textArr);
for (var i = 0; i < placeholderArr.length; i++) {
if (placeholderArr[i].getAttribute('placeholder') !== null) { new Placeholder(placeholderArr[i]);
}
}
} };
}(window));

二、解决placeholder在ios上的小坑

  • 在苹果高版本iPhone6、7 上发现了一个问题,当设置placeholder显示的字体大小的时候,会被遮挡掉一部分

解决方法:先设置input 里面的字体大小需要大于placeholder的字体大小

三、让 placeholder 换行

  1. 在input 里面很少用到,input就只有一行而已,多行的话就会使用textarea标签,确实在textarea标签里面如何让placeholder换行是一个麻烦事,由于不同浏览器兼容性还不一样. 这里提供一个简单的实现方法
    jq_watermark,一个基于jQuery的小插件,min版本才2.8KB 使用方式 导入jQuery,导入jq_watermark,
    jq_watermark在github上的下载地址 给textarea 加上名为 jq_watermark 的class

<textarea name="" class="jq_watermark" cols="110" rows="10" required placeholder="第一行<br/>第二行<br/>第三行"></textarea>

原文链接:https://blog.csdn.net/qq_2959...

三、解决 placeholder 兼容性之修改样式

::-webkit-input-placeholder { /* WebKit, Blink, Edge */
color: #909;
}
:-moz-placeholder { /* Mozilla Firefox 4 to 18 */
color: #909;
}
::-moz-placeholder { /* Mozilla Firefox 19+ */
color: #909;
}
:-ms-input-placeholder { /* Internet Explorer 10-11 */
color: #909;
}

========================================================================


解决input 中placeholder的那些神坑的更多相关文章

  1. css设置input中placeholder字体

    设置input中placeholder字体颜色 input::-webkit-input-placeholder {color:@a;} input:-moz-placeholder {color:@ ...

  2. Hmtl5 <input>中placeholder属性(新属性)

    Hmtl5 <input>中placeholder属性(新属性) 一.定义和用法 placeholder 属性提供可描述输入字段预期值的提示信息(hint). 该提示会在输入字段为空时显示 ...

  3. 解决IE中placeholder的兼容问题

    定义和用法 placeholder 属性提供可描述输入字段预期值的提示信息(hint). 该提示会在输入字段为空时显示,并会在字段获得焦点时消失. 注释:placeholder 属性适用于以下的 &l ...

  4. 解决input中智能提示框onblur与onclick冲突的问题

    背景: 制作一个类似百度输入法的智能提示框, 其中当关键词输入进来时,会有智能提示展开,实际需求是当点击智能提示框的汉字时,输入框中自动补全并关闭智能提示, 当点击其他区域时,智能提示框自动隐藏,如下 ...

  5. 解决input标签placeholder属性浏览器兼容性问题的一种方法

    为文本框input添加文字输入提示,H5为input提供了一个placeholder属性.在支持H5的浏览器中,用此属性设置输入提示,简单方便,但是对于IE8以下版本,都不支持placeholder属 ...

  6. 移动端 华为手机 input中placeholder垂直居中失效

    为一个app写了一个嵌套的提现页面,效果如下图 input给定宽高,给了line-heigh,在浏览器查看效果正常,placeholder内容以及光标显示都是垂直居中的, IOS显示正常, Andro ...

  7. placeholder 解决UITextField中placeholder和text文本同时显示的问题

    TextField都使用了placeholder属性,但在代码中又设置了text属性,因此ViewController会同时显示placeholder文本和text文本. 这个问题让我彻底崩溃.按道理 ...

  8. 解决HTML5中placeholder属性兼容性的JQuery插件

    //调用方法 $(function () {   $(".pHolder").jason(); }); //HTML代码 <input type="text&quo ...

  9. 设置input中placeholder的样式(placeholder设置字体)

    方法: 代码示例: .input::-webkit-input-placeholder { font-size: 3.73333333vw; color: #cccccc; } .input:-moz ...

随机推荐

  1. C# 虚拟键盘

    [DllImport("user32.dll")] public static extern void keybd_event(byte bVk, byte bScan, uint ...

  2. Elastic Stack学习

    原文链接 Elastic Stack简称ELK,在本教程你将学习如何快速搭建并运行Elastic Stack. 首先你要安装核心开源产品: Elasticsearch: Kibana: Beats: ...

  3. Golang基础(5):Go语言反射规则

    Go语言反射规则 - The Laws of Reflection 转:http://my.oschina.net/qbit/blog/213720 原文地址:http://blog.golang.o ...

  4. springboot mybatis下临时表的创建和删除,可用于查重去重

    /** * 创建临时表 */ @Update({"drop temporary table if exists ${tableName};", "create tempo ...

  5. Doker部署Jmeter(一) 目标服务器部署Jmeter监控容器

    用jmeter插件监控服务器性能之前也有提到:https://www.cnblogs.com/betterbb/p/11285022.html 这里主要记录一下docker上的部署,所需的3个插件可以 ...

  6. APP自动化测试,判断页面与预期是否相同

    自动化测试中,有时需要验证页面跳转是否正常 1.选择appium实现,因为要填写appPcakage和appActivity,只能验证一个单独的APP,在自身APP上各个页面是否跳转正常 例如:焦点从 ...

  7. LeetCode.1025-除数游戏(Divisor Game)

    这是小川的第382次更新,第411篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第244题(顺位题号是1025).Alice和Bob轮流玩游戏,Alice首先出发. 最初 ...

  8. 应用安全 - 工具 - 中间件 - Apache - Apache Tika - 漏洞汇总

    CVE-2016-6809 Date2016 类型远程代码执行 影响范围Apache Tika 1.6-1.13 CVE-2018-1335 Date2018 类型命令注入 影响范围Tika-serv ...

  9. 【神经网络与深度学习】【VS开发】【CUDA开发】VS2013 配置CUDNN V4 DEMO

    VS2013 配置CUDNN V4 DEMO 众所周知,当前主流深度学习的实现中调用的底层API都是cudnn,自己做项目需要开发深度学习模块时,也需要调用cudnn库,因此熟悉cudnn库是很有必要 ...

  10. linux shutdown 命令 关机 重启

    关机 shutdown -h now 重启 shutdown -r now