文本框 placeholder 兼容IE7 IE 8不能显示的效果

来源:互联网 发布:c语言移位操作 编辑:程序博客网 时间:2024/06/05 03:55

IE7 IE 8 对文本框的  placeholder 属性不支持

提出一种解决方案

//**前提 需引用 jquery.js文件

<script type="text/javascript">

$(function(){

       handlePlaceholderForIE();

});

 function handlePlaceholderForIE() {
            // placeholder attribute for ie7 & ie8
            if (jQuery.browser.msie && jQuery.browser.version.substr(0, 1) <= 9) {

                // ie7&ie8
                jQuery('input[placeholder], textarea[placeholder]').each(function () {
                    var input = jQuery(this);
                    jQuery(input).val(input.attr('placeholder'));
                    jQuery(input).focus(function () {
                        if (input.val() == input.attr('placeholder')) {
                            input.val('');
                        }
                    });
                    jQuery(input).blur(function () {
                        if (input.val() == '' || input.val() == input.attr('placeholder')) {
                            input.val(input.attr('placeholder'));
                        }
                    });
                });
            }
        }

</script>

<input    type="text" id="txt"  placeholder="请输入您的账号"     autocomplete="off"" />

0 0
原创粉丝点击