美文网首页
手动添加placeholder属性

手动添加placeholder属性

作者: HeroMeikong | 来源:发表于2017-12-27 22:44 被阅读0次

接受一个form对象

function resetFields(whichform) {
    if (Modernizr.input.placeholder) return;
    for (var i=0;i<whichform.elements.length;i++) {
        var element = whichform.elements[i];
        if (element.type == "submit") continue;
        var check = element.placeholder || element.getAttribute('placeholder');
        if (!check) continue;
        element.onfocus = function () {
            var text = this.placeholder || this.getAttribute('placeholder');
            if (this.value == text) {
                this.className = '';
                this.value = "";
            }
        }
        element.onblur = function () {
            if (this.value == "") {
                this.className = 'placeholder';
                this.value = this.placeholder || this.getAttribute('placeholder');
            }
        }
        element.onblur();
    }
}

可通过遍历传入所有form对象

function prepareForms() {
    for (var i=0;i<document.forms.length;i++) {
        var thisform = document.forms[i];
        resetFields(thisform);
    }
}

相关文章

网友评论

      本文标题:手动添加placeholder属性

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