JS如何获取文本框中被选择的内容?

如题,注意是“被选择”的部分内容假如有如下form:<formname="FA"><inputtype="text"name="TA"value=""><inputtyp... 如题,注意是“被选择”的部分内容
假如有如下form:
<form name="FA">
<input type="text" name="TA" value="">
<input type="text" name="TB" value="">
</form>
在“TA”中输入一段文字,然后鼠标选择其中部分文字,选择后的内容传到“TB”中,那么在“TA”中的JS事件该怎么写?
展开
 我来答
溜须拍马关云长
推荐于2016-01-11 · TA获得超过1708个赞
知道大有可为答主
回答量:2443
采纳率:0%
帮助的人:0
展开全部
<input type="text" id="TA" value="" onmouseup="document.getElementById('TB').value=getRangeById('TA')">
<input type="text" id="TB" value="">

<script type="text/javascript">
<!--
function getRangeById(id)
{
var word='';
if (document.selection){o=document.selection.createRange();if(o.text.length>0)word=o.text;}else{
o=document.getElementById(id); p1=o.selectionStart;p2=o.selectionEnd;
if (p1||p1=='0'){if(p1!=p2)word=o.value.substring(p1,p2);}}
return word;
}
//-->
</script>
维思迈
2009-02-23 · TA获得超过223个赞
知道小有建树答主
回答量:255
采纳率:0%
帮助的人:194万
展开全部
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>

<body>

<script type="text/javascript">
<!--
function getFieldSelection(select_field)
{
word='';
if (document.selection) {
var sel = document.selection.createRange();
if (sel.text.length > 0) {
word = sel.text;
}
}
else if (select_field.selectionStart || select_field.selectionStart == '0') {
var startP = select_field.selectionStart;
var endP = select_field.selectionEnd;
if (startP != endP) {
word = select_field.value.substring(startP, endP);
}
}
return word;
}
//-->
</script>
<textarea id="t_field" rows="3" cols="20">你选选看看</textarea><br />
<span onmousedown="alert(getFieldSelection(document.getElementById('t_field')))" style="color:#6633FF;cursor:pointer">(Span_onmousedown)</span>
<span onclick="alert(getFieldSelection(document.getElementById('t_field')))" style="color:#6633FF;cursor:pointer">(Span_onclick)</span>
<button onclick="alert(getFieldSelection(document.getElementById('t_field')))">button_click</button>

</body>
</html>

你自己改改就行了
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式