html5的placeholder属性(IE如何兼容placeholder属性)

html5的placeholder属性(IE如何兼容placeholder属性) 

2013-01-05 10:26:06|  分类: web学习 |  标签:html  js  ie  placeholder  |举报 |字号 订阅 html

 
 

placeholder属性,IE对其的支持表示无奈,firefox、google chrome表示毫无压力。html5

 

HTML5对Web Form作了许多加强,好比input新增的type类型、Form Validation等。Placeholder是HTML5新增的另外一个属性,当input或者textarea设置了该属性后,该值的内容将做为灰字提示显示在文本框中,当文本框得到焦点时,提示文字消失。之前要实现这效果都是用JavaScript来控制才能实现:jquery

<input id="t1" type="text" placeholder="请输入文字" />web

因为placeholder是个新增属性,目前只有少数浏览器支持,如何检测浏览器是否支持它呢?(更多HTML5/CSS3特性检测能够访问)chrome

 

默认提示文字是灰色的,能够经过CSS来改变文字样式:浏览器

 

兼容其余不支持placeholder的浏览器:ide

 

介绍一个超强的让IE下支持placeholder的属性插件,代码以下:学习

 

 $(document).ready(function () {
            var doc = document,
                inputs = doc.getElementsByTagName('input'),
                supportPlaceholder = 'placeholder' in doc.createElement('input'),
                placeholder = function (input) {
                    var text = input.getAttribute('placeholder'),
                        defaultValue = input.defaultValue;
                    if (defaultValue == '') {
                        input.value = text
                    }
                    input.onfocus = function () {
                        if (input.value === text) {
                            this.value = ''
                        }
                    };
                    input.onblur = function () {
                        if (input.value === '') {
                            this.value = text
                        }
                    }
                };
            if (!supportPlaceholder) {
                for (var i = 0, len = inputs.length; i < len; i++) {
                    var input = inputs[i],
                        text = input.getAttribute('placeholder');
                    if (input.type === 'text' && text) {
                        placeholder(input)
                    }
                }
            }
        });
this

 

直接把代码复制下来,保存成一个js文件引用便可,根本不用再作任何处理,超级方便~google

 此文源于http://lidrema.blog.163.com/blog/static/209702148201305101844932/

相关文章
相关标签/搜索