使用window.getSelection()获取div中选中文字内容及位置

div添加一个弹出事件: 

        $(document).ready(function () {
            $("#marked-area").mouseup(function (e) {
                $scope.mark.saveSelectionText();
            });
        });
            saveSelectionText: function () {
                if (window.getSelection) {
             $scope.mark.selectionText[0] = window.getSelection().toString();
             $scope.mark.selectionText[1] = window.getSelection().anchorOffset; //开始位置
             $scope.mark.selectionText[2] = window.getSelection().focusOffset;  //结束位置

                } else if (document.selection && document.selection.type != "Control") {  //ie9以下的支持,可不考虑
                    $scope.mark.selectionText[0] = document.selection.createRange().text;
                }
       }

灵感来源: http://www.webhek.com/post/get-selected-text-with-javascript.html

原文地址:https://www.cnblogs.com/XHappyness/p/8205418.html