簡體   English   中英

ie9中的Angular JS占位符問題

[英]Angular js placeholder issues in ie9

我在部分頁面中添加了占位符,如下所示,

<input name="name" type="text" placeholder="Enter name" ng-class="{'error':form.name.$invalid}" ng-model="Name" required />

並實現了文本框的客戶端驗證。

if (this.name.$error.regEx) {
        $scope.errorList.push({              
            Message: "Invalid name"
        });
    }

我已經為正則表達式創建了指令。 該代碼在chrome和IE 10中可以正常工作。但是在IE 9中,如果我在文本框中復制並粘貼正確的名稱,則會引發客戶端驗證錯誤(因為它也會使用文本框)。 誰能幫我解決該問題?

IE9不支持placeholder屬性,因為它是HTML5屬性。 thomseddon提供了很好的解決方案

angular.module('test', [])
.directive('placeholder', function($timeout){
    var i = document.createElement('input');
    if ('placeholder' in i) {
        return {}
    }
    return {
        link: function(scope, elm, attrs){
            if (attrs.type === 'password') {
                return;
            }
            $timeout(function(){
                elm.val(attrs.placeholder);
                elm.bind('focus', function(){
                    if (elm.val() == attrs.placeholder) {
                        elm.val('');
                    }
                }).bind('blur', function(){
                    if (elm.val() == '') {
                        elm.val(attrs.placeholder);
                    }
                });
            });
        }
    }
});

資料來源: https : //gist.github.com/thomseddon/4703810

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM