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); },