對IE8兼容的處理 password框的特殊處理 (placeholder屬性)

   /** 對IE8兼容的處理 password框的特殊處理  對IE8兼容的處理
     * 1.建立一個text框
     * 2.獲取焦點和失去焦點的時候切換
     */
    // 判斷瀏覽器是否支持placeholder屬性
    function isSupportPlaceholder() {
        var input = document.createElement('input');
        return 'placeholder' in input;
    }瀏覽器

    $(function() {
        if(!isSupportPlaceholder()) {
            $('input[type="password"]').each(
                    function() {
                        var pwdField = $(this);
                        var pwdVal = pwdField.attr('placeholder');
                        var pwdId = pwdField.attr('id');
                        // 重命名該input的id爲原id後跟1
                        pwdField.after('<input id="' + pwdId +'1" type="text" style="color: darkgrey" value='+pwdVal+' autocomplete="off" />');
                        var pwdPlaceholder = $('#' + pwdId + '1');
                        pwdPlaceholder.show();
                        pwdField.hide();ide

                        pwdPlaceholder.focus(function(){
                            pwdPlaceholder.hide();
                            pwdField.show();
                            pwdField.focus();
                        });this

                        pwdField.blur(function(){
                            if(pwdField.val() == '') {
                                pwdPlaceholder.show();
                                pwdField.hide();
                            }
                        });
                    }
            );
        }
    });input

相關文章
相關標籤/搜索