Calculating text selection offsets in nest elements in Javascript

前端 未结 5 1918
无人及你
无人及你 2021-01-05 14:58

The Problem

I am trying to figure out the offset of a selection from a particular node with javascript.

Say I have the following HTML

5条回答
  •  天命终不由人
    2021-01-05 15:59

    you can use the following java script code:

    var text = window.getSelection();
    var start = text.anchorOffset;
    alert(start);
    var end = text.focusOffset - text.anchorOffset;
    alert(end);
    

提交回复
热议问题