完蛋了,什么都忘了!如何用js在textarea的光标处插入文字 txt.focus();document.selection.createRange().text += "text"; 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> <meta name="GENERATOR" content="Microsoft FrontPage 4.0"> <meta name="ProgId" content="FrontPage.Editor.Document"> <title>光标位置</title> <style> INPUT{border: 1 solid #000000} BODY,TABLE{font-size: 10pt} </style> </head> <body> <table border="0" width="700" cellspacing="0" cellpadding="0"> <tr> <td width="479" rowspan="7"> 点击 TextArea 实现光标定位 <p> <textarea rows="7" cols="49" id="box" onclick=tellPoint()>怎么样很简单吧,如果我们再把文件上传和此结合起来,就会产生很多使用的功能。如:在用上传组件上传多个文件后,可以对其多个文件打包,以提供给其他人zip文件下载或者上传一个zip文件后,可以利用程序对其解压,以提供给其他人单个文件下载很方便用户吧!我会在以后的文章里把这两种变化写出来!文中难免有疏漏的地方,请大家多多指正!</textarea> <script> function movePoint() { var pn = parseInt(pnum.value); if(isNaN(pn)) return; var rng = box.createTextRange(); rng.moveStart("character",pn); rng.collapse(true); rng.select(); returnCase(rng) } function tellPoint() { var rng = event.srcElement.createTextRange(); rng.moveToPoint(event.x,event.y); rng.moveStart("character",-event.srcElement.value.length) pnum.value = rng.text.length returnCase(rng) } function returnCase(rng) { bh.innerText = rng.boundingHeight; bl.innerText = rng.boundingLeft; bt.innerText = rng.boundingTop; bw.innerText = rng.boundingWidth; ot.innerText = rng.offsetTop; ol.innerText = rng.offsetLeft; t.innerText = rng.text; } function selectText(sp,ep) { sp = parseInt(sp) ep = parseInt(ep) if(isNaN(sp)||isNaN(ep)) return; var rng = box.createTextRange(); rng.moveEnd("character",-box.value.length) rng.moveStart("character",-box.value.length) rng.collapse(true); rng.moveEnd("character",ep) rng.moveStart("character",sp) rng.select(); returnCase(rng); } var rg = box.createTextRange(); function findText(tw) { if(tw=="") return; var sw = 0; if(document.selection) { sw = document.selection.createRange().text.length; } rg.moveEnd("character",box.value.length); rg.moveStart("character",sw); if(rg.findText(tw)) { rg.select(); returnCase(rg); } if(rg.text!=tw) { alert("已经搜索完了") rg = box.createTextRange() } } </script> </p> <p></p> 光标位置:<input type="text" value="0" id="pnum" size="8"> <input type="button" onclick="movePoint()" value="移动光标到指定位置"> <p></p> 选择指定范围:<input type="text" size="9" id="sbox"> -- <input type="text" size="9" id="ebox"> <input type="button" onclick="selectText(sbox.value,ebox.value)" value="选择"> <p></p> 选择查找字符 :<input type="text" value="" id="cbox" size="8"> <input type="button" onclick="findText(cbox.value)" value="查找下一个并选择"> </td> <td width="217">boundingHeight: <span id="bh"></span></td> </tr> <tr> <td width="217">boundingWidth: <span id="bw"></span></td> </tr> <tr> <td width="217">boundingTop: <span id="bt"></span></td> </tr> <tr> <td width="217">boundingLeft: <span id="bl"></span></td> </tr> <tr> <td width="217">offsetLeft: <span id="ol"></span> </td> </tr> <tr> <td width="217">offsetTop: <span id="ot"></span> </td> </tr> <tr> <td width="217">text: <span style="position: absolute; z-index: 10" id="t"></span> </td> </tr> </table> </body> </html> 这个问题偶也遇到过,偶的应用是在光标处插入签名档,偶的签名档放在select的option-value中,不得已啊后面就简单了,建立选区document.selection.createRange()粘贴签名内容select.valueexeCommand(Paste)就可以了~~~~~~~~~~~~但是得判断一下光标位置,否则可能就插在document中了 怎么这样都插入不了,高手帮帮我//插入文字function addsign(cr) {alert(cr)document.selection.createRange().box += "cr";box.focus();}光标处插入文字 :<input type="text" value="" id="sign" size="8"> <input type="button" onclick="addsign(sign.value)" value="光标处插入"> JQuery dialog JS不用正则表达式如何验证整型。 JS构造树问题 POST字符大小限制如何解决 小弟求救,在地址栏输入Javascript代码来实现页面的跳转,表单的填写和提交,但是不成功 javascritp 显示鼠标的当前位置 在站点中不起作用 但不在站点却起作用了 |zyciis| 如何判断一段文字,是否有这个方法 谢谢 关于路径指向的问题? 鼠标事件的几个问题,在线请高手指教! 怎样获知按钮被点击的事件? DreamweaverMX2004自动生成的JAVASCRIPT代码查询日期型字段报错问题 请问怎样下载框架网站的网页?
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>光标位置</title>
<style>
INPUT{border: 1 solid #000000}
BODY,TABLE{font-size: 10pt}
</style>
</head>
<body>
<table border="0" width="700" cellspacing="0" cellpadding="0">
<tr>
<td width="479" rowspan="7">
点击 TextArea 实现光标定位
<p>
<textarea rows="7" cols="49" id="box" onclick=tellPoint()>怎么样很简单吧,如果我们再把文件上传和此结合起来,就会产生很多使用的功能。如:
在用上传组件上传多个文件后,可以对其多个文件打包,以提供给其他人zip文件下载
或者上传一个zip文件后,可以利用程序对其解压,以提供给其他人单个文件下载
很方便用户吧!我会在以后的文章里把这两种变化写出来!文中难免有疏漏的地方,请大家多多指正!
</textarea>
<script>
function movePoint()
{
var pn = parseInt(pnum.value);
if(isNaN(pn))
return;
var rng = box.createTextRange();
rng.moveStart("character",pn);
rng.collapse(true);
rng.select();
returnCase(rng)
}
function tellPoint()
{
var rng = event.srcElement.createTextRange();
rng.moveToPoint(event.x,event.y);
rng.moveStart("character",-event.srcElement.value.length)
pnum.value = rng.text.length
returnCase(rng)
} function returnCase(rng)
{
bh.innerText = rng.boundingHeight;
bl.innerText = rng.boundingLeft;
bt.innerText = rng.boundingTop;
bw.innerText = rng.boundingWidth;
ot.innerText = rng.offsetTop;
ol.innerText = rng.offsetLeft;
t.innerText = rng.text;
} function selectText(sp,ep)
{
sp = parseInt(sp)
ep = parseInt(ep)
if(isNaN(sp)||isNaN(ep))
return;
var rng = box.createTextRange();
rng.moveEnd("character",-box.value.length)
rng.moveStart("character",-box.value.length)
rng.collapse(true);
rng.moveEnd("character",ep)
rng.moveStart("character",sp)
rng.select();
returnCase(rng);
}
var rg = box.createTextRange();
function findText(tw)
{
if(tw=="")
return;
var sw = 0;
if(document.selection)
{
sw = document.selection.createRange().text.length;
}
rg.moveEnd("character",box.value.length);
rg.moveStart("character",sw); if(rg.findText(tw))
{
rg.select();
returnCase(rg);
}
if(rg.text!=tw)
{
alert("已经搜索完了")
rg = box.createTextRange()
}
}
</script>
</p>
<p></p>
光标位置:<input type="text" value="0" id="pnum" size="8"> <input type="button" onclick="movePoint()" value="移动光标到指定位置">
<p></p>
选择指定范围:<input type="text" size="9" id="sbox"> -- <input type="text" size="9" id="ebox"> <input type="button" onclick="selectText(sbox.value,ebox.value)" value="选择">
<p></p>
选择查找字符 :<input type="text" value="" id="cbox" size="8"> <input type="button" onclick="findText(cbox.value)" value="查找下一个并选择">
</td>
<td width="217">boundingHeight: <span id="bh"></span></td>
</tr>
<tr>
<td width="217">boundingWidth: <span id="bw"></span></td>
</tr>
<tr>
<td width="217">boundingTop: <span id="bt"></span></td>
</tr>
<tr>
<td width="217">boundingLeft: <span id="bl"></span></td>
</tr>
<tr>
<td width="217">offsetLeft: <span id="ol"></span> </td>
</tr>
<tr>
<td width="217">offsetTop: <span id="ot"></span> </td>
</tr>
<tr>
<td width="217">text: <span style="position: absolute; z-index: 10" id="t"></span> </td>
</tr>
</table>
</body>
</html>
document.selection.createRange()
粘贴签名内容select.valueexeCommand(Paste)
就可以了~~~~~~~~~~~~
但是得判断一下光标位置,否则可能就插在document中了
function addsign(cr) {alert(cr)document.selection.createRange().box += "cr";
box.focus();
}
光标处插入文字 :<input type="text" value="" id="sign" size="8"> <input type="button" onclick="addsign(sign.value)" value="光标处插入">