![](/img/trans.png)
[英]Is there a way to get the offset of the selected text within an input box in IE?
[英]Get selected text in an input box
是否可以使用jQuery或vanilla JavaScript在网站的输入框中获取所选文本?
我尝试过var selectedText = window.getSelection().toString();
,但此代码仅获取段落中的文本而不是输入框中的文本。
编辑:也许我不清楚,我想从一个我没有创建的网站上获取文本。 我正在构建Chrome扩展程序,我需要从网站的输入框中获取文本。
带有解决方案查找下面
function disp() { var text = document.getElementById("text"); var t = text.value.substr(text.selectionStart, text.selectionEnd - text.selectionStart); alert(t); }
<TEXTAREA id="text">Hello, How are You?</TEXTAREA><BR> <INPUT type="button" onclick="disp()" value="show selected" />
如果您不介意使用jQuery插件,可以使用这个来完成它http://hamaapaja.github.io/jquery.selection/
它很灵活(您可以将它用于输入和段落)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.