---
title: "Textarea在光标停留处插入文字"
author: "cai555"
date: "2010-03-17T15:51:58Z"
tags: []
categories: ["Web前端"]
slug: "textarea"
draft: false
lastmod: "2010-03-17T15:52:38Z"
id: 618161
url: "https://cai555.iteye.com/blog/618161"
---

```
<!-- 

function Insert(str) { 
var obj = document.getElementById('content'); 
if(document.selection) { 
obj.focus(); 
var sel=document.selection.createRange(); 
document.selection.empty(); 
sel.text = str; 
} else { 
var prefix, main, suffix; 
prefix = obj.value.substring(0, obj.selectionStart); 
main = obj.value.substring(obj.selectionStart, obj.selectionEnd); 
suffix = obj.value.substring(obj.selectionEnd); 
obj.value = prefix + str + suffix; 
} 
obj.focus(); 
} 
--> 
```

 兼容IE、firefox