在输入框中获取所选文本

Sar*_*nda 4 html javascript jquery google-chrome google-chrome-extension

是否可以使用jQuery或vanilla JavaScript在网站的输入框中获取所选文本?

我已尝试过var selectedText = window.getSelection().toString();,但此代码只获取段落中的文本而不是输入框中的文本.


编辑:也许我不清楚,我想从一个我没有创建的网站上获取文本.我正在构建Chrome扩展程序,我需要从网站的输入框中获取文本.

Man*_*ddy 7

带有解决方案查找下面

function disp() {
  var text = document.getElementById("text");
  var t = text.value.substr(text.selectionStart, text.selectionEnd - text.selectionStart);
  alert(t);
}
Run Code Online (Sandbox Code Playgroud)
<TEXTAREA id="text">Hello, How are You?</TEXTAREA><BR>
<INPUT type="button" onclick="disp()" value="show selected" />
Run Code Online (Sandbox Code Playgroud)