• angular自定义指令解决IE89不支持input的placeholder属性


    下面代码实测通过,直接copy到本地运行即可。

    <!DOCTYPE html>
    <html>
    
        <head>
            <meta charset="UTF-8">
            <title></title>
            <script src="https://cdn.bootcss.com/angular.js/1.2.14/angular.min.js"></script>
            <style>
                .yf-input-container {
                    position: relative;
                    font-size: 14px;
                    margin-bottom: 10px;
                }
                
                .yf-input-container input {
                    height: 20px;
                    line-height: 20px;
                }
            </style>
        </head>
    
        <body>
            <div ng-controller="superPoolCtrl">
                <div class="yf-input-container">
                    姓名:<input type="text" ng-model="name" yf-placeholder="请输入姓名" />
                </div>
                <div class="yf-input-container">
                    电话:<input type="text" ng-model="tel" yf-placeholder="请输入电话" />
                </div>
                <div>
                    <div class="yf-input-container">
                        职位:<input type="text" ng-model="job" yf-placeholder="请输入职位" />
                    </div>
                </div>
            </div>
            <script>
                var moduleName = 'bidmeApp';
                var app = angular.module(moduleName, [])
                    .controller('superPoolCtrl', ['$scope', '$rootScope', '$timeout', function($scope, $rootScope, $timeout) {
                        $timeout(function() {
                            $scope.tel = "13800138000";
                        }, 1000);
                        $scope.name = "http://www.cnblogs.com/chenchenghua/";
                    }])
                    .directive('yfPlaceholder', function() {
                        return {
                            restrict: 'A', //指令取属性
                            scope: false, //与父级共享作用域
                            replace: true,
                            require: '',
                            template: function(elem, attr) {
                                return '<input />'
                            },
                            link: function(scope, element, attr) {
                                var inputEle = element[0];
                                var supportPlaceholder = "placeholder" in document.createElement("input") ? true : false;
                                if(supportPlaceholder) {
                                    inputEle.setAttribute('placeholder', attr.yfPlaceholder);
                                } else {
                                    function insertAfter(newElement, targetElement) { // newElement是要追加的元素 targetElement 是指定元素的位置 
                                        var parent = targetElement.parentNode; // 找到指定元素的父节点 
                                        if(parent.lastChild == targetElement) { // 判断指定元素的是否是节点中的最后一个位置 如果是的话就直接使用appendChild方法 
                                            parent.appendChild(newElement, targetElement);
                                        } else {
                                            parent.insertBefore(newElement, targetElement.nextSibling);
                                        };
                                    };
                                    var node = angular.element(document.createElement('span'));
                                    node.css({
                                        position: 'absolute',
                                        zIndex: '1',
                                        color: '#A9A9A9',
                                        top: inputEle.offsetTop + 'px',
                                        left: inputEle.offsetLeft + 'px',
                                         inputEle.offsetWidth + 'px',
                                        height: inputEle.offsetHeight + 'px',
                                        lineHeight: inputEle.offsetHeight + 'px',
                                        textIndent: '5px',
                                        cursor: 'text'
                                    }).text(attr.yfPlaceholder);
                                    angular.element(inputEle).after(node);
    
                                    node.on('click', function(a, b, c) { //点击placeholder,隐藏。input获取焦点
                                        node.css({
                                            "display": 'none'
                                        });
                                        inputEle.focus();
                                    });
    
                                    angular.element(inputEle).on('blur', function() { //input失去焦点时,做判断
                                        if(inputEle.value.length < 1) {
                                            node.css({
                                                "display": 'block'
                                            });
                                        }
                                    });
    
                                    angular.element(inputEle).on('focus', function() { //input失去焦点时,做判断
                                        if(inputEle.value.length < 1) {
                                            node.css({
                                                "display": 'none'
                                            });
                                        }
                                    });
    
                                    scope.$watch(attr.ngModel, function(newVal, oldVal) { //监听,比如开始有值或者异步请求回显,placeholder隐藏
                                        if(!!newVal && newVal.length > 0) {
                                            node.css({
                                                "display": 'none'
                                            });
                                        }
                                    });
                                }
                            }
                        };
                    });
                angular.bootstrap(document.body, [moduleName]);
            </script>
        </body>
    
    </html>

    --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    欢迎转载,转载请注明作者:飘飞的夏秋 和出处 http://www.cnblogs.com/chenchenghua/p/6757736.html

  • 相关阅读:
    Win10新建文件不自动刷新
    解决 Win10 系统新建文件夹后需手动刷新才能显示
    新建的文件需要刷新才能看见怎么办?
    win7、win10系统电脑开机后小键盘灯不亮怎么办?
    VMware Workstation 将虚拟机挂起后,电脑会很卡,SCSI转换成IDE就可以了
    如何把VMware Workstation使用的虚拟SCSI磁盘转换成虚拟IDE硬盘
    bat批处理文件怎么将路径添加到path环境变量中
    在Windows下使用svn命令行教程及svn命令行的解释
    php正确解码javascript中通过escape编码后的字符
    采集/自动登录啊都可以用这两个方法实现 asp.net
  • 原文地址:https://www.cnblogs.com/chenchenghua/p/6757736.html
Copyright © 2020-2023  润新知