在textarea光标处插入内容

//在textarea光标处插入内容
	function Insert(str) {
		var obj = document.getElementsByTagName("textarea")[0];
		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();
	}

  

posted @ 2013-09-30 10:11  黑暗骑士之“闪”  阅读(294)  评论(1编辑  收藏  举报