簡體   English   中英

獲取用戶在區域中選擇的所有文本節點

[英]Get all the text node that a user selected in an area

是否可以獲取用戶在區域中選擇的所有文本節點?

我嘗試選擇對象( window.getSelection() )來獲取開始文本節點和結束節點的anchorNodefocusNode

現在,我正在嘗試獲取起始文本節點與末尾之間的所有文本節點。 此問題還需要關注兩個節點之間的子節點和父節點。

是的,您可以使用document.selection類(IE)或selectionStartselectionEnd屬性(Firefox / Chrome)

var $ = function(id) { return document.getElementById(id); };

$('get').onclick = function() {
  var text = $('text'),
      selection = '';

  if (typeof document.selection !== 'undefined')
  {
    text.focus();
    var sel = document.selection.createRange();
    selection = sel.text;
  }
  else
  {
    var d = text.selectionStart,
        e = text.selectionEnd;
    for (var i = d; i < e; i++)
    {
      selection += text.value[i];
    }
  }
  alert ("Selected content:\n" + selection);
};

工作演示

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM