Storing Highlighted text in a variable

前端 未结 3 1337
栀梦
栀梦 2021-01-16 11:35

Is there a javascript function that will allow me to capture the text that is currently highlighted with the cursor and store it in a variable? I\'ve been trying document.se

3条回答
  •  长发绾君心
    2021-01-16 11:59

    function getSelectedText() {
        if (window.getSelection) {
            return "" + window.getSelection();
        } else if (document.selection && document.selection.type == "Text") {
            return document.selection.createRange().text;
        }
        return "";
    }
    

提交回复
热议问题