首页 > 代码库 > 【JS】IE兼容placeholder

【JS】IE兼容placeholder

直接上代码:

   $(document).ready(function () {
        var doc = document,
                textareas = doc.getElementsByTagName(‘textarea‘),
                supportPlaceholder = ‘placeholder‘ in doc.createElement(‘textarea‘),

                placeholder = function (textarea) {
                    var text = textarea.getAttribute(‘placeholder‘),
                            defaultValue = textarea.defaultValue;
                    if (defaultValue =http://www.mamicode.com/= ‘‘) {
                        textarea.value = text
                    }
                    textarea.onfocus = function () {
                        if (textarea.value =http://www.mamicode.com/== text) {
                            this.value = http://www.mamicode.com/‘‘
                        }
                    };
                    textarea.onblur = function () {
                        if (textarea.value =http://www.mamicode.com/== ‘‘) {
                            this.value =http://www.mamicode.com/ text
                        }
                    }
                };

        if (!supportPlaceholder) {
            for (var i = 0, len = textareas.length; i < len; i++) {
                var textarea = textareas[i],
                        text = textarea.getAttribute(‘placeholder‘);
                if ( text) {
                    placeholder(textarea);
                }
            }
        }
    });

textarea可以换成input

【JS】IE兼容placeholder