光标位置插入文本

摘自 https://blog.csdn.net/gisredevelopment/article/details/29255147

纯记录,没啥可说的

<html>   
<head>   
<script type='text/javascript'>   
function test(str){  
    var tc = document.getElementById("mytextarea");  
    var tclen = tc.value.length;  
    tc.focus();  
    if(typeof document.selection != "undefined")  
    {  
      document.selection.createRange().text = str;    
    }  
    else  
    {  
      tc.value = 
      tc.value.substr(0, tc.selectionStart) +
      str +
      tc.value.substring(tc.selectionStart, tclen);
    }  
}  
</script>   
</head>   
<body>  
<textarea rows=5 name=s1 cols=27  id="mytextarea">aabb</textarea>   
<input type=button onclick="test('这是需要加入的文字')" />   
</body>
</html>  

猜你喜欢

转载自blog.csdn.net/u011393161/article/details/80736575
今日推荐