急急急,求各位大侠帮忙,用js实现文本域累加操作,前提是:光标定位到哪里,输入的值就要在光标的地方显示?

解决方案 »

  1.   

    jsp页面上有两行,第一行显示+、-、*、/、(、)等运算符按钮,第二行是文本域,我在文本域中要输入一些表达式如:a+b*c>0,运算符都是通过点击上面的按钮显示的,现在我把光标放到最前面给表达式加上(),效果如下:(a+b)*c>0 就是光标移动到哪,点击运算符就在哪显示,我实现的效果只是在后面进行累加的。请高手指点!!! 
      

  2.   

    如果是的话:
    <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>估计你是想要这样的代码