js & right click menu & 鼠标滑词

公孙国兴
2023-12-01

js & right click menu & 鼠标滑词


    // 鼠标滑词
    mouseSlipGetWords() {
        const getSelectionText = () => {
            let selectedTextWithTrim = ``;
            if (window.getSelection){
                // all modern browsers and IE9+
                selectedTextWithTrim = window.getSelection().toString().trim();
                // console.log(`you selected text with trim() =`, selectedTextWithTrim);
            } else {
                console.error(`鼠标滑词 selectedText is empty!`);
            }
            return selectedTextWithTrim;
        };
        let textarea = document.querySelector(`#audit-common-handle-text`);
        let that = this;
        if (document.addEventListener) {
            // IE >= 9; other browsers
            textarea.addEventListener('contextmenu', function(e) {
                let value = getSelectionText() || "";
                if (value) {
                    that.$hMessage.success(`滑动选取的单词: ${value}`);
                    // that.isNewAddLabel = true;
                    // that.tableLoading = true;
                    that.commonHandleModalTableData = [];
                    that.searchValue = value;
                    // update value with auto click
                    that.getLabelCategories(``, ``);
                    that.onClickSearch();
                    setTimeout(() => {
                        // show modal with delay
                        that.modalNew.showModal = true;
                    }, 0);
                } else {
                    console.log(`selected text is empty!`);
                    that.$hMessage.error(`滑动选取的单词不可为空`);
                    // this.$hMessage.info(`滑动选取的单词不可为空`);
                }
                e.preventDefault();
            }, false);
        } else {
            // IE < 9
            document.attachEvent('oncontextmenu', function() {
                // alert("You've tried to open context menu");
                window.event.returnValue = false;
            });
        }
    },

转载于:https://www.cnblogs.com/xgqfrms/p/10274559.html

 类似资料: