惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
U
Unit 42
Google DeepMind News
Google DeepMind News
博客园 - 司徒正美
Y
Y Combinator Blog
F
Fortinet All Blogs
云风的 BLOG
云风的 BLOG
T
Tailwind CSS Blog
G
Google Developers Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
罗磊的独立博客
D
DataBreaches.Net
T
The Blog of Author Tim Ferriss
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
MyScale Blog
MyScale Blog
N
Netflix TechBlog - Medium
Microsoft Security Blog
Microsoft Security Blog
GbyAI
GbyAI
P
Proofpoint News Feed
Jina AI
Jina AI
B
Blog RSS Feed
腾讯CDC
阮一峰的网络日志
阮一峰的网络日志
D
Docker

博客园 - 非我

目前主流的四大浏览器内核Trident、Gecko、WebKit以及Presto 随手记录: MVC自定义提交form 在文本框的光标处插入指定的文本(兼容IE6和Firefox) Javascript对象的比较 javascript对象的深拷贝 对mootools的颜色选择器做个小改进 用户中心 - 博客园 检查矩阵是否有线性相关性 - 非我 - 博客园 WOW笑话,笑死我了 动态修改/添加CSS文件链接 使用.net验证控件遇到的小麻烦 (转)40种网站设置技巧 访问者模式-Visitor 学习与思考 对Response.End的疑问 累啊~用记事本写的小程序 Observer pattern 观察者模式 艾薇尔新歌GrilFriend的MV Command Pattern - 命令模式(ASP.NET版) 动态控件的问题
在文本框的光标处插入指定的文本(另一种方式)
非我 · 2008-07-17 · via 博客园 - 非我

前面一篇是插入固定值,更多的考虑到类似在插入表情这样的需求,今天又碰到另一个朋友问我另一种方式,于是顺便研究了下另一种在光标处插入文本的办法。

var TextAreaPosition = function(tb)
{
    
this.textBox = tb;
    
this.start = 0;
    
this.end = 0;
    
    
this.InitEvent = function(obj)
    
{
        function savePos()
        
{
            obj.savePosition(
this);
        }

        
        
this.textBox.onkeydown = savePos;
        
this.textBox.onkeyup = savePos;
        
this.textBox.onmousedown = savePos;
        
this.textBox.onmouseup = savePos;
        
this.textBox.onfocus = savePos;
    }


    
this.InsertString = function(str)
    
{        
        var pre 
= this.textBox.value.substr(0this.start);
        var post 
= this.textBox.value.substr(this.end);
        
this.textBox.value = pre + str + post;
    }


    
this.savePosition = function(textBox)
    
{
        
if(typeof(this.textBox.selectionStart) == "number")
        
{
            
this.ff15();
        }

        
else if(document.selection)
        
{
            
this.ie6(); 
        }

    }

    
    
this.ff15 = function()
    
{
        
this.start = this.textBox.selectionStart;
        
this.end = this.textBox.selectionEnd;
    }

    
    
this.ie6 =function()
    
{
        var range 
= document.selection.createRange();

        
if(range.parentElement().id == this.textBox.id)
        
{
            var range_all 
= document.body.createTextRange();
            range_all.moveToElementText(
this.textBox);
            
            
for (this.start=0; range_all.compareEndPoints("StartToStart", range) < 0this.start++)
            
{
                range_all.moveStart(
'character'1);
            }

            
            
for (var i = 0; i <= this.start; i ++)
            
{
                
if (this.textBox.value.charAt(i) == '\n'this.start++;
            }


            var range_all 
= document.body.createTextRange();
            range_all.moveToElementText(
this.textBox);

            
for (this.end = 0; range_all.compareEndPoints('StartToEnd', range) < 0this.end ++)
            
{
                 range_all.moveStart(
'character'1);
            }


            
for (var i = 0; i <= this.end; i ++)
            
{
                 
if (this.textBox.value.charAt(i) == '\n'this.end ++;
            }

        }

    }

    
    
this.InitEvent(this);
}

测试:

<html>
<head>
<title>TEST</title>
<script src="1.js" type="text/javascript"></script>

<script type="text/javascript">
    
    var tp;

    function Add()
    
{
        tp.InsertString(document.getElementById(
"inputtext").value);
    }

    
    window.onload 
= function()
    
{
        tp 
= new TextAreaPosition(document.getElementById("ta"));
    }

    
    
</script>
</head>
<body>
<textarea id="ta" rows="14" cols="50"></textarea><br/>
<input type="text" id="inputtext" />
<input type="button" onclick="Add()" value="Add Text"/>
</body>
</html>