码迷,mamicode.com
首页 > Web开发 > 详细

【JS】IE兼容placeholder

时间:2016-11-22 12:05:06      阅读:186      评论:0      收藏:0      [点我收藏+]

标签:nts   ie兼容   creat   class   his   default   length   var   style   

直接上代码:

   $(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 == ‘‘) {
                        textarea.value = text
                    }
                    textarea.onfocus = function () {
                        if (textarea.value === text) {
                            this.value = ‘‘
                        }
                    };
                    textarea.onblur = function () {
                        if (textarea.value === ‘‘) {
                            this.value = 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

标签:nts   ie兼容   creat   class   his   default   length   var   style   

原文地址:http://www.cnblogs.com/hectorE/p/6088393.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!