资讯专栏INFORMATION COLUMN

解决input 中placeholder的那些神坑

ingood / 445人阅读

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

**昨天后台小哥哥提到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属性,

提取demo 链接:https://pan.baidu.com/s/1AMl6... 密码:zs9c
解决方案二: 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/98250.html

相关文章

  • 解决input placeholder那些神坑

    摘要:昨天后台小哥哥提到无法显示问题,我这边总结一下,顺便写个小文章分享给大家。。 **昨天后台小哥哥提到placehold无法显示问题,我这边总结一下,顺便写个小文章分享给大家。。** ============================================== 一、解决ie9以下input 无placeholder问题 解决方案一:jquery实现 当浏览器不支持...

    chnmagnus 评论0 收藏0
  • 解决input placeholder那些神坑

    摘要:昨天后台小哥哥提到无法显示问题,我这边总结一下,顺便写个小文章分享给大家。。 **昨天后台小哥哥提到placehold无法显示问题,我这边总结一下,顺便写个小文章分享给大家。。** ============================================== 一、解决ie9以下input 无placeholder问题 解决方案一:jquery实现 当浏览器不支持...

    bigdevil_s 评论0 收藏0
  • 前端遇到那些技术难点

    摘要:目前主流的屏幕或者。产生原因在中,手指按住屏幕上下拖动,会触发事件。或者可以加入我的开发交流群相互学习,我们会有专业的技术答疑解惑如果你觉得这篇文章对你有点用的话,麻烦请给我们的开源项目点点不胜感激 移动端兼容css篇移动端的 1px问题描述:1px 的边框。在高清屏下,移动端的 1px 会很粗。产生原因:首先先要了解一个...

    番茄西红柿 评论0 收藏2637
  • 小程序开发那些小坑

    摘要:解决方法用组件替换组件,用微信小程序的实现点击切换效果除此之外,在中也不能使用组件。接口更改问题微信小程序最近被吐槽最多的一个更改,就是用户使用开发和体验版时不会弹出授权,正式版不受影响。 最近专门做小程序开发中,跟大家分享下遇到那些不得不处理的小坑,欢迎指正 1.小程序用 WxParse 在手机上不能正确解析 html 代码并显示 解决办法: 这个是 wxparse 代码的一个...

    CODING 评论0 收藏0
  • 小程序开发那些小坑

    摘要:解决方法用组件替换组件,用微信小程序的实现点击切换效果除此之外,在中也不能使用组件。接口更改问题微信小程序最近被吐槽最多的一个更改,就是用户使用开发和体验版时不会弹出授权,正式版不受影响。 最近专门做小程序开发中,跟大家分享下遇到那些不得不处理的小坑,欢迎指正 1.小程序用 WxParse 在手机上不能正确解析 html 代码并显示 解决办法: 这个是 wxparse 代码的一个...

    jerryloveemily 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<