Is it possible mask input using a different placeholder?

北战南征 提交于 2019-12-04 07:57:49

I'm thinking you can use another tag to simulate placeholder, maybe the code here is not very good, but I just provide another thought.

app.directive("myPlaceholder", ['$compile', function($compile){
return {
    restrict: 'A',
    link: function(scope, elem, attr) {
        attr.$observe('myPlaceholder', initialize);
        var mask = '__/__/____';

        function initialize(value) {
            // label is not clickable in IE, that's the reason why we use span tag
            var fakePlaceholder = angular.element('<span class="placeholder">' + value + '</span>');
            // click placeholder to focus the input
            fakePlaceholder.on('click', function(){
                elem.focus();
            });
            elem.before(fakePlaceholder);
            $compile(fakePlaceholder)(scope);

            elem.on('focus', function() {
                fakePlaceholder.hide();
            }).on('blur', function() {
                if (elem.val() === mask) {
                    fakePlaceholder.show();
                }
            });
        }
    }
};
}]);

demo on http://jsfiddle.net/XS4R6/19/ (jQuery required)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!