ionic js input光标重新定位

来源:互联网 发布:怎么用阿里云 caffe 编辑:程序博客网 时间:2024/05/19 11:45

html 代码 : 

<body ng-app="order" ng-controller="orderXqCtl"><ion-view><ion-content>...<input type="text" style="padding:0;" ng-model="card" ng-click=""  ng-keyup="duan($event)" id="kh">                        ...


js 代码:

.controller('bankEditCtl', function ($scope, $http, $ionicBackdrop) {        ...        $scope.duan = function (e) {            var keycode = window.event ? e.keyCode : e.which;            console.log(keycode);            if (keycode != 13 && keycode != 8 && keycode != 46) {            var index_focus = e.target.selectionStart;//获取当前鼠标光标                document.getElementById("kh").value = document.getElementById("kh").value.replace(/\s/g, '').replace(/(\d{4})(?=\d)/g, "$1 ");//执行空格填充之后,光标会被定位到末尾                if (index_focus>4 && index_focus%5 == 0){//防止填充空格的影响                index_focus+=1;                }                document.getElementById("kh").setSelectionRange(index_focus, index_focus); //重新定位光标位置            }        };

0 0
原创粉丝点击