How can I deselect text using Javascript or jQuery?

后端 未结 3 960
孤独总比滥情好
孤独总比滥情好 2021-02-06 23:37

I have a draggable (jQuery UI) element with \"canceled\" text on it. Here\'s what I mean:

$(\'#main\').draggable({
        cancel: \'#main>* *\',
        star         


        
相关标签:
3条回答
  • 2021-02-06 23:50

    To deselect everything you can use:

    document.getSelection().removeAllRanges();
    
    0 讨论(0)
  • 2021-02-07 00:12

    Clear document selection document.selection.empty();

    Something like:

     $('#main').draggable({
         cancel: '#main>* *',
         start: function(){
          // deselect text
          document.selection.empty();
         }
     });
    
    0 讨论(0)
  • 2021-02-07 00:12

    jQuery UI now includes .disableSelection(), which you can use to prevent the text inside your elements from being selected. The API documentation on it isn't hugely useful, but: http://api.jqueryui.com/disableSelection/

    As an aside, Eric G's answer proved supremely helpful for me when creating my own "text highlighting" tool - as Lawrence remarked, it won't throw out an error if no text has been selected.

    0 讨论(0)
提交回复
热议问题