xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

js & right click menu & 鼠标滑词 All In One

js & right click menu & 鼠标滑词 All In One

js 实现鼠标右键滑词功能

window.getSelection

contextmenu


    // 鼠标滑词
    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/ 查看原创文章!

refs



©xgqfrms 2012-2021

www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!


posted @ 2019-01-15 22:04  xgqfrms  阅读(412)  评论(1编辑  收藏  举报