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

来源:百度知道 编辑:UC知道 时间:2024/05/06 14:42:12
如题,注意是“被选择”的部分内容
假如有如下form:
<form name="FA">
<input type="text" name="TA" value="">
<input type="text" name="TB" value="">
</form>
在“TA”中输入一段文字,然后鼠标选择其中部分文字,选择后的内容传到“TB”中,那么在“TA”中的JS事件该怎么写?

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="