在输入框中获取选定的文本 [英] Get selected text in an input box

查看:118
本文介绍了在输入框中获取选定的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在网站的输入框中获取选定的文字,例如通过使用jQuery?



我尝试过使用 var kelet = 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 =文字>你好,你好吗?< / TEXTAREA>< BR>< / code>< INPUT type =buttononclick =disp()value =show selected


Is it possible to get the selected text in an input box of a website e.g. by using jQuery?

I have tried with var kelet = window.getSelection().toString(); but this code get only the text in a paragraph and not in an input box.


maybe I was unclear, I want to get the text from a website that I didn't creat. I'm building a chrome extension and I need to get the text from input box of a website.

解决方案

Came with solution Find below

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" />

这篇关于在输入框中获取选定的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆