---
title: "focus after selection ——ie和Firefox的差别"
author: "hite"
date: "2010-03-15T13:01:42Z"
tags: []
categories: ["Web前端"]
slug: "focus-after-selection-ie-firefox"
draft: false
lastmod: "2010-03-15T13:01:42Z"
id: 616011
url: "https://hite.iteye.com/blog/616011"
---

今天使用fckeditor做一个变量插入的功能，也就是点击按钮后，在fckeditor里插入相应的值。当然了，这段代码是插入textarea或者textfield的； function insertNotifyArgments(t){

\[code="js"] var str = t.value;  
var ubb=$('reccamTemplateVO.content');  
var ubbLength=ubb.value.length;  
ubb.focus();  
if(typeof document.selection !=&quot;undefined&quot;){  
document.selection.createRange().text=str;  
}else{  
ubb.value=ubb.value.substr(0,ubb.selectionStart)+str+ubb.value.substring(ubb.selectionStart,ubbLength);  
}

}

\[code]  
使用到这种可编辑的iframe里是不行。

 

搜索一圈又以下代码。

```
	function insertNotifyArgments(str){
		var wnd = $("reccamTemplateVO.content___Frame").contentWindow.document.getElementsByTagName("IFRAME")[0].contentWindow;
		 wnd.focus();
	
		 if(typeof document.selection !="undefined"){
			 wnd.document.selection.createRange().text=str;
		}else{
			 wnd.document.execCommand("InsertHTML", false,str);
		}
		 
	}
```

 

但是问题来了，在Firefox下是正确的，ie下每次插入都是最前面，

 

仔细观察后发现，在Firefox里一个域里有selection的时候，你点击了另外一个域，使这个域focus，上一个域的selection是存在的所以，使用上面的代码位置是正确的。但是在ie下一旦触发了blur，selection就丢失了。。

 

 

像我的这个功能点，操作是这样的，选择一个selection，点击一个下拉框，选中一项。此时ie下selection域的onblur就已经触发了。。。失败。

 

所以只能在点击按钮的时候插入。因为selection后，点击按钮、链接的时候，触发onclick的，此时onblur没有触发，，所以需要修改为按钮。

 

 

....解决