how to get selection inside a div using jquery/javascript

前端 未结 3 998
难免孤独
难免孤独 2020-11-30 12:21

there are lots of code to get selection in a page, but i want a code to get selection inside a div, if the selection is outside of my div, the function must return empty str

3条回答
  •  时光取名叫无心
    2020-11-30 12:44

    You can use:

    var node = window.getSelection ?        
           window.getSelection().focusNode.parentNode:
           document.selection.createRange().parentElement();
    

    to get the element in which the selection end.

    Then you can determine if that element is inside your div.

提交回复
热议问题