js 获取文本框选中内容

/**
         * 获取选中内容
         * @param id
         * @returns {string|*}
         */
        function getSelectText(id) {
            var t = document.getElementById(id);
            if (window.getSelection) {
                if (t.selectionStart != undefined && t.selectionEnd != undefined) {
                    return t.value.substring(t.selectionStart, t.selectionEnd);
                } else {
                    return "";
                }
            } else {
                return document.selection.createRange().text;
            }
        }

调用  getSelectText("qst")     参数为 文本框 ID

posted on 2021-12-16 09:23  码农at突泉  阅读(408)  评论(0)    收藏  举报