vue 獲取div中選中文字內容及位置

使用window.getSelection()獲取div中選中文字內容及位置

window.getSelection().toString() //選中內容

window.getSelection().anchorOffset; //開始位置

window.getSelection().focusOffset;  //結束位置

如果需要兼容ie9以下:

      if (window.getSelection) {
        // window.getSelection().toString();
        // window.getSelection().anchorOffset; //開始位置
        // window.getSelection().focusOffset; //結束位置
      } else if (document.selection && document.selection.type != "Control") {
        //ie9以下的支持,可不考慮
        // document.selection.createRange().text;
      }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章