光標(biāo)選區(qū)API Selection

兼容性

IE9以上基本都已經(jīng)支持該API

Paste_Image.png

selection 實(shí)例

  • 獲取selction window.selection方法
  • anchor 用戶操作開始的地方
  • focus 用戶操作結(jié)束的地方
  • range 是包含element文本節(jié)點(diǎn)的html片段,通常來說用戶選擇的selection 包含一個(gè)range實(shí)例, 多實(shí)例的功能只有火狐支持。

實(shí)例屬性

屬性名稱 類型 描述
anchorNode NodeElement 用戶選擇區(qū)域開始的節(jié)點(diǎn)
anchorOffset Number 如果anchorNode是文本節(jié)點(diǎn),anchorOffset表示用戶選擇開始的字符相對(duì)節(jié)點(diǎn)內(nèi)容首個(gè)字符的偏移,如果是element 則表示,該節(jié)點(diǎn)相對(duì)element子元素的位置。
forcusNode NodeElement 用戶選擇區(qū)域結(jié)束的節(jié)點(diǎn)
forcusOffset Number 如果forcusNode是文本節(jié)點(diǎn),forcusOffset表示用戶選擇開始的字符相對(duì)節(jié)點(diǎn)內(nèi)容首個(gè)字符的偏移,如果是element 則表示,該節(jié)點(diǎn)相對(duì)element子元素的位置。
isCollapsed Boolean 用戶操作的起始點(diǎn)和終點(diǎn)是否重合(可以用來判斷是否是有效操作,為true時(shí)表示取消操作)
rangeCount Number 返回選區(qū)內(nèi)range的個(gè)數(shù)

實(shí)例方法

下面介紹的方法是chrome已經(jīng)支持的方法

  • getRangeAt() 返回 當(dāng)全選中的range對(duì)象中的某一個(gè)
  • collapse(parentNode, offset)銷毀現(xiàn)有的selection 實(shí)例并用 parentNode.childNodes[offset]作為anchorfocus生成新的selection來替換。
  • extend(parentNode, offset) 移動(dòng)focus到指定位置不改變anchor
var sel = window.getSelection();
sel.extend(sel.focusNode, sel.focusOffset + 5);
  • selectAllChildren(parentNode) parentNode的所有子元素被加入到selection中替換掉了原有的。
  • deleteFromDocument() 從document中刪除
  • toString 返回當(dāng)前被選中的內(nèi)容的文本
  • containsNode(aNode,aPartlyContained) 表明文本aNode是否在Seleciton中 aPartlyContained === false 要求aNode必須在seleciton中 aPartlyContained === true 部分在即可
window.getSelection().containsNode(document.body, true)
>>true

復(fù)制內(nèi)容到剪貼板

// preViewEl: HTMLParagraphElement;
copy = (e: Event) => {
   let range,
       selection;
   try{
       selection = window.getSelection();
       range = document.createRange();
       range.selectNodeContents(this.preViewEl);
       selection.removeAllRanges();
       selection.addRange(range);
       document.execCommand('copy');
       this.preViewEl.blur();
   }catch(e){
       debugger;
   }
}

注意
preViewEl 一定是需要展示在頁面上的元素,不能使用display隱藏, 可以定位到可視區(qū)域的外面。

// 雙擊復(fù)制內(nèi)容組件

// @flow
import React, { Component } from 'react';

class SelectAllText extends Component{
    props: {
        children: ReactElement
    };
    rootEl: HTMLDivElement;
    select = (e) =>{
        try{
            const selection = window.getSelection();
            const range = document.createRange();
            const targetEl = this.rootEl.firstElementChild;
            if (!targetEl) return;
            range.selectNodeContents(targetEl);
            selection.removeAllRanges();
            selection.addRange(range);
            document.execCommand('copy');
            targetEl.blur();
        }catch(e){
            $.message.show({
                type: 'error',
                msg: '復(fù)制內(nèi)容到剪貼板失敗'
            })
        }
    }
    render(){
        return (
            <div onDoubleClick={this.select} ref={el => this.rootEl = el}>
                {this.props.children}
            </div>
        )
    }
}
export default SelectAllText;
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 用途 在瀏覽器環(huán)境中,可以使用document.getSelection方法獲取到Selection對(duì)象,Sele...
    憑添一筆慘淡閱讀 5,319評(píng)論 0 2
  • 1. Java基礎(chǔ)部分 基礎(chǔ)部分的順序:基本語法,類相關(guān)的語法,內(nèi)部類的語法,繼承相關(guān)的語法,異常的語法,線程的語...
    子非魚_t_閱讀 34,740評(píng)論 18 399
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,616評(píng)論 19 139
  • 最開始讀了貓叔推薦的《書都不會(huì)讀,你還想成功》后,暗自下決心要100天讀完33本書,但是在讀完不到10本書之后就放...
    環(huán)盈閱讀 175評(píng)論 0 1
  • 從10月份下旬開始擴(kuò)充并閱讀和學(xué)習(xí)方法相關(guān)的書籍,已經(jīng)整理出來了相關(guān)書單 可以將書單里的書劃分為兩類: 一、讀書的...
    木易_1992閱讀 16,559評(píng)論 0 18

友情鏈接更多精彩內(nèi)容