开发者

javascript get selected html elements [duplicate]

This question already has answers here: How to get selected html text with javascr开发者_运维问答ipt? [duplicate] (5 answers) Closed 9 years ago.

User is on a webpage , uses his mouse to select stuff, how can i use javascript to know what has been selected?


To get the raw text currently highlighted on the page you can do something like this:

function getSelectedText() {
    return window.getSelection ? window.getSelection() 
                               : document.selection.createRange().text;
}

Check an example of the above code here.

More info:

  • DOM Selection
  • IE document.selection


If you're talking about the user dragging his mouse over text so that it's highlighted, use the Selection and Range objects (for Mozilla) and Selection and TextRange objects (for IE).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜