js实现文本域累加操作 急急急,求各位大侠帮忙,用js实现文本域累加操作,前提是:光标定位到哪里,输入的值就要在光标的地方显示? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 jsp页面上有两行,第一行显示+、-、*、/、(、)等运算符按钮,第二行是文本域,我在文本域中要输入一些表达式如:a+b*c>0,运算符都是通过点击上面的按钮显示的,现在我把光标放到最前面给表达式加上(),效果如下:(a+b)*c>0 就是光标移动到哪,点击运算符就在哪显示,我实现的效果只是在后面进行累加的。请高手指点!!! 如果是的话:<html> <head> <title>javascript获取/设置 文本框/文本域中的光标位置</title> <script type="text/javascript"> function getTxt1CursorPosition(){ var oTxt1 = document.getElementById("txt1"); var cursurPosition=-1; if(oTxt1.selectionStart){//非IE浏览器 cursurPosition= oTxt1.selectionStart; }else{//IE var range = document.selection.createRange(); range.moveStart("character",-oTxt1.value.length); cursurPosition=range.text.length; } alert(cursurPosition); } function setTxt1CursorPosition(i){ var oTxt1 = document.getElementById("txt2"); var cursurPosition=-1; if(oTxt1.selectionStart){//非IE浏览器 oTxt1.selectionStart=i; }else{//IE var range = oTxt1.createTextRange(); range.move("character",i); range.select(); } } function getTa1CursorPosition(){ var evt =window.event?window.event:getTa1CursorPosition.caller.arguments[0]; var oTa1 = document.getElementById("ta1"); var cursurPosition=-1; if(oTa1.selectionStart){//非IE浏览器 cursurPosition= oTa1.selectionStart; }else{//IE var range = oTa1.createTextRange(); range.moveToPoint(evt.x,evt.y); range.moveStart("character",-oTa1.value.length); cursurPosition=range.text.length; } alert(cursurPosition); } function setTa1CursorPosition(i){ var oTa2 = document.getElementById("ta2"); if(oTa2.selectionStart){//非IE浏览器 oTa2.selectionStart=i; oTa2.selectionEnd=i; }else{//IE var range = oTa2.createTextRange(); range.move("character",i); range.select(); } } </script> </head><body><hr /><textarea id="ta1" rows="" cols="" style="width:100%; height:90px;" onclick="getTa1CursorPosition()">abcdefghijklmnopqrstuvwxyz点击我获取文本域的光标位置</textarea><textarea id="ta2" rows="" cols="" style="width:100%; height:90px;" onclick="setTa1CursorPosition(30)">abcdefghijklmnopqrstuvwxyz点击我设置文本域的光标位置为30</textarea></body></html>估计你是想要这样的代码 JS动态控制列表框值 可以向上移动一个,但是第二个怎么移?? 大家有没有js时间倒计时的,防止刷新的 css调用.htc文件 在Asp.Net里用System.Web.HttpUtility.UrlEncode(s)编码过的字符串,如何用javascript读。 关于查询问题 jQuery.validate的accept 验证失败 如何解决open一个窗口导致session丢失? 利用Jacascript客户端脚本,实现网页在不同季节呈现不同的背景色 谁能给我看看这个小小的程序呢? 用JavaScript进行页面跳转验证的问题 关于数组.sort()的排序方法
<html>
<head> <title>javascript获取/设置 文本框/文本域中的光标位置</title> <script type="text/javascript">
function getTxt1CursorPosition(){
var oTxt1 = document.getElementById("txt1");
var cursurPosition=-1;
if(oTxt1.selectionStart){//非IE浏览器
cursurPosition= oTxt1.selectionStart;
}else{//IE
var range = document.selection.createRange();
range.moveStart("character",-oTxt1.value.length);
cursurPosition=range.text.length;
}
alert(cursurPosition);
} function setTxt1CursorPosition(i){
var oTxt1 = document.getElementById("txt2");
var cursurPosition=-1;
if(oTxt1.selectionStart){//非IE浏览器
oTxt1.selectionStart=i;
}else{//IE
var range = oTxt1.createTextRange();
range.move("character",i);
range.select();
}
} function getTa1CursorPosition(){
var evt =window.event?window.event:getTa1CursorPosition.caller.arguments[0];
var oTa1 = document.getElementById("ta1");
var cursurPosition=-1;
if(oTa1.selectionStart){//非IE浏览器
cursurPosition= oTa1.selectionStart;
}else{//IE
var range = oTa1.createTextRange();
range.moveToPoint(evt.x,evt.y);
range.moveStart("character",-oTa1.value.length);
cursurPosition=range.text.length;
}
alert(cursurPosition);
} function setTa1CursorPosition(i){
var oTa2 = document.getElementById("ta2");
if(oTa2.selectionStart){//非IE浏览器
oTa2.selectionStart=i;
oTa2.selectionEnd=i;
}else{//IE
var range = oTa2.createTextRange();
range.move("character",i);
range.select();
}
}
</script>
</head><body>
<hr />
<textarea id="ta1" rows="" cols="" style="width:100%; height:90px;" onclick="getTa1CursorPosition()">abcdefg
hijklmn
opqrst
uvwxyz
点击我获取文本域的光标位置</textarea><textarea id="ta2" rows="" cols="" style="width:100%; height:90px;" onclick="setTa1CursorPosition(30)">abcdefg
hijklmn
opqrst
uvwxyz
点击我设置文本域的光标位置为30</textarea>
</body>
</html>估计你是想要这样的代码