Selecting text in an element (akin to highlighting with your mouse)

后端 未结 16 1620
孤街浪徒
孤街浪徒 2020-11-21 05:58

I would like to have users click a link, then it selects the HTML text in another element (not an input).

By \"select\" I mean the same way you would select

16条回答
  •  不思量自难忘°
    2020-11-21 06:10

    here is another simple solution to get the selected the text in the form of string, you can use this string easily to append a div element child into your code:

    var text = '';
    
    if (window.getSelection) {
        text = window.getSelection();
    
    } else if (document.getSelection) {
        text = document.getSelection();
    
    } else if (document.selection) {
        text = document.selection.createRange().text;
    }
    
    text = text.toString();
    

提交回复
热议问题