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事件该怎么写? 展开
假如有如下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>
<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="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>
你自己改改就行了
<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>
你自己改改就行了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询