摘要:昨天后台小哥哥提到无法显示问题,我这边总结一下,顺便写个小文章分享给大家。。
</>复制代码
**昨天后台小哥哥提到placehold无法显示问题,我这边总结一下,顺便写个小文章分享给大家。。**
==============================================
一、解决ie9以下input 无placeholder问题</>复制代码
解决方案一:jquery实现
当浏览器不支持placeholder属性时,克隆一个和界面相同的input框,将placeholder的值设置为其value值,覆盖在界面input框所在位置,并将界面上的input隐藏掉
调用方法:
</>复制代码
$(#selector).placeholder();(selector泛指css 的 id选择器)
当文本框type=password时,引用此placeholder方案会使暗文密码变成明文密码
如果input文本框使用了bootstrap 行高会高一点,要修改placeholder内的文字样式 可在placeholder.js里中添加style属性如:
</>复制代码
如果是普通的input 则无需添加style属性,
</>复制代码
解决方案二: js/jQuery实现
实现思路:
1、首先判断浏览器是否支持placeholder属性,如果不支持则使用模拟placeholder
2、创建一个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 换行在input 里面很少用到,input就只有一行而已,多行的话就会使用textarea标签,确实在textarea标签里面如何让placeholder换行是一个麻烦事,由于不同浏览器兼容性还不一样. 这里提供一个简单的实现方法
jq_watermark,一个基于jQuery的小插件,min版本才2.8KB 使用方式 导入jQuery,导入jq_watermark,
jq_watermark在github上的下载地址 给textarea 加上名为 jq_watermark 的class
</>复制代码
原文链接: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;
}
========================================================================
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/53057.html
摘要:昨天后台小哥哥提到无法显示问题,我这边总结一下,顺便写个小文章分享给大家。。 **昨天后台小哥哥提到placehold无法显示问题,我这边总结一下,顺便写个小文章分享给大家。。** ============================================== 一、解决ie9以下input 无placeholder问题 解决方案一:jquery实现 当浏览器不支持...
摘要:昨天后台小哥哥提到无法显示问题,我这边总结一下,顺便写个小文章分享给大家。。 **昨天后台小哥哥提到placehold无法显示问题,我这边总结一下,顺便写个小文章分享给大家。。** ============================================== 一、解决ie9以下input 无placeholder问题 解决方案一:jquery实现 当浏览器不支持...
摘要:目前主流的屏幕或者。产生原因在中,手指按住屏幕上下拖动,会触发事件。或者可以加入我的开发交流群相互学习,我们会有专业的技术答疑解惑如果你觉得这篇文章对你有点用的话,麻烦请给我们的开源项目点点不胜感激 移动端兼容css篇移动端的 1px问题描述:1px 的边框。在高清屏下,移动端的 1px 会很粗。产生原因:首先先要了解一个...
摘要:解决方法用组件替换组件,用微信小程序的实现点击切换效果除此之外,在中也不能使用组件。接口更改问题微信小程序最近被吐槽最多的一个更改,就是用户使用开发和体验版时不会弹出授权,正式版不受影响。 最近专门做小程序开发中,跟大家分享下遇到那些不得不处理的小坑,欢迎指正 1.小程序用 WxParse 在手机上不能正确解析 html 代码并显示 解决办法: 这个是 wxparse 代码的一个...
摘要:解决方法用组件替换组件,用微信小程序的实现点击切换效果除此之外,在中也不能使用组件。接口更改问题微信小程序最近被吐槽最多的一个更改,就是用户使用开发和体验版时不会弹出授权,正式版不受影响。 最近专门做小程序开发中,跟大家分享下遇到那些不得不处理的小坑,欢迎指正 1.小程序用 WxParse 在手机上不能正确解析 html 代码并显示 解决办法: 这个是 wxparse 代码的一个...
阅读 3881·2021-11-23 09:51
阅读 4726·2021-11-15 11:37
阅读 3622·2021-09-02 15:21
阅读 2826·2021-09-01 10:31
阅读 1018·2021-08-31 14:19
阅读 939·2021-08-11 11:20
阅读 3442·2021-07-30 15:30
阅读 1775·2019-08-30 15:54