手动添加placeholder属性

2017-12-27  本文已影响0人  HeroMeikong

接受一个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);
    }
}
上一篇下一篇

猜你喜欢

热点阅读