美文网首页
vue项目解决IE9不支持placeholder

vue项目解决IE9不支持placeholder

作者: 刘其瑞 | 来源:发表于2020-09-22 10:19 被阅读0次

通过vue的自定义全局指令 Vue.directive 来控制。
由于项目中使用到了elementUi ,所以以解决elementUi组件的input为例

<el-input v-support v-model="value" placeholder="请输入姓名"></el-input>

自定义指令代码

    Vue.directive("support", {
        inserted: function (el, binding, vnode) {
            console.log(el, 'el')
            // 如果兼容placeholder则终止
            if (("placeholder" in document.createElement("input"))) {
                return;
            }
            // select暂时不支持(input为只读状态时)
            var isReadOnly = el.getAttribute("readOnly");
            if (isReadOnly) return

            // 由于第三方input组件都包了一个外层
            var input = el.querySelector("input");
            var placeholder = input.getAttribute("placeholder");
            var span = document.createElement("span");

            // .ie-placeholder 可根据类名自定义样式
            span.className = "ie-placeholder";
            span.innerText = placeholder;
            // 位置设置
            span.style.left = input.offsetLeft + '4' + "px";
            input.parentNode.style.position = "relative";
            input.parentNode.appendChild(span);
            el.addEventListener('click', function (event) {
                if (event.target.nodeName == 'SPAN') {
                    // span.style.display = "none";
                    input.focus()
                }
            })
            span.addEventListener('click', function (event) {
                // span.style.display = "none";
                input.focus()
            })
            input.onblur = function (event) {
                if (!event.target.value) {
                    span.style.display = "inline";
                }
            };
            input.oninput = function (event) {
                if (event.target.value) {
                    span.style.display = "none";
                } else {
                    span.style.display = "inline";
                }
            }
        },

        // 开始更新
        unbind: function (el) {
            var input = el.querySelector("input");
            input.onfocus = input.onblur = input.oninput = null;
        }
    })

可根据ie-placeholder修改css样式

.ie-placeholder {
    font-size: 14px;
    color: #9c9c9c;
}

效果图:

image.png

相关文章

  • vue项目解决IE9不支持placeholder

    通过vue的自定义全局指令 Vue.directive 来控制。由于项目中使用到了elementUi ,所以以解决...

  • 解决placeholder的兼容性

    placeholder在不支持html5的低版本的浏览器中,placeholder属性是无效的,ie9及以下的ie...

  • vue+axios+vue-router兼容ie9

    参考:Vue 兼容 ie9 的全面解决方案 前序 项目突然要求兼容ie9...可是用到的技术栈对ie9似乎不是很友...

  • 浏览器兼容性问题解决方案汇总

    解决 ie9 以下浏览器对 html5 新增标签不识别的问题 解决 ie9 以下浏览器不支持 CSS3 Media...

  • css attr()

    应用场景:IE9 placeholder无效 好用记录一下

  • 前端 兼容性

    ie7 ie8 ie9 textarea placeholder (http://www.cnblogs.com/...

  • IE9踩坑之旅

    近期做的一个项目需要兼容IE9,以前没有兼容IE9的经历,想着也不难,结果遇坑无数,现整理如下: 1.不支持fle...

  • 没事写点工作中遇到的问题

    项目需要解决兼容ie9 首先看下项目在ie9下显示的报错内容 先不管css报错问题,我们知道即使css报错,页面应...

  • Node Sass does not yet support y

    在运行一个 vue-element-admin 的项目时,报错如下 node sass 不支持当前的环境,解决方法...

  • 不能console

    IE8下 浏览器不打开控制台使用console.log 是报错的 。 解决方法 //解决 IE8、IE9 不支持 ...

网友评论

      本文标题:vue项目解决IE9不支持placeholder

      本文链接:https://www.haomeiwen.com/subject/bpquxctx.html