1. 程式人生 > >怎麽獲取textarea中選中文字

怎麽獲取textarea中選中文字

creat ext document range length .get ring element save

textarea設置select="saveSelectionText()"

//保存選中內容
            saveSelectionText: function () {
                var focusEle = document.activeElement.tagName;
                if (focusEle === ‘TEXTAREA‘) {
                    if (window.getSelection) {
                        $scope.mark.selectionText[0] = window.getSelection().toString();
                    } 
else if (document.selection && document.selection.type != "Control") { $scope.mark.selectionText[0] = document.selection.createRange().text; } var ele = document.getElementById(‘textarea2‘); if ($scope.mark.selectionText[0].length == 0) { $scope.mark.selectionText[
0] = ($scope.mark.Content || ‘‘).substring(ele.selectionStart, ele.selectionEnd) if (!$scope.mark.selectionText[0]) { $scope.mark.selectionText = []; return false; } } $scope.mark.selectionText[
1] = ele.selectionStart.toString(); $scope.mark.selectionText[2] = (ele.selectionEnd - ele.selectionStart).toString(); } },

這樣,$scope.mark.selectionText保存了選中的文字,第一項為選中文字,第二項為開始位置,第三項為選中的長度。

blur時清空選中內容:blur=‘resetSelectionText()‘

//清空選中內容
            resetSelectionText: function () {
                $timeout(function () {
                    $scope.mark.selectionText = [];
                }, 500);
            },

怎麽獲取textarea中選中文字