zoukankan      html  css  js  c++  java
  • 怎么获取textarea中选中文字

    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保存了选中的文字,第一项为选中文字,第二项为开始位置,第三项为选中的长度。

    如果textarea换成div,只需给div添加一个鼠标弹起事件

    $(document).ready(function () {
                $("#divID").mouseup(function (e) {
                    $scope.mark.saveSelectionText();
                });
            });
  • 相关阅读:
    ROW_NUMBER() OVER (PARTITION BY yy ORDER BY zz) in Linq
    Oracle-sql分页方法
    Lambda
    ISNULL做简单的显示字段逻辑
    Select2使用方法汇总
    mysqldump 定时备份数据(全量)
    ubuntu16.10下安装erlang和RabbitMQ
    XShell连接本地Ubuntu虚拟机
    Haroopad 安装到 Mac OSX
    Swagger 生成API文档
  • 原文地址:https://www.cnblogs.com/XHappyness/p/8072366.html
Copyright © 2011-2022 走看看