<html> <head> <script language="javascript"> function getCursortPosition (ctrl) {//获取光标位置函数 var CaretPos = 0; // IE Support if (document.selection) { ctrl.focus (); var Sel = document.selection.createRange (); Sel.moveStart ('c
js获取光标位置 var TT = { /* * 获取光标位置 * @Method getCursorPosition * @param t element * @return number */ getCursorPosition:function(t){ if (document.selection) { t.focus(); var ds = document.selection; var range = ds.createRange(); var stored_range = ran
<html><head><title>TEST</title><style>body,td { font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;}</style><script type="text/javascript"> var start = 0; var end = 0; function add()
/** * 获取选中文字 * 返回selection,toString可拿到结果,selection含有起始光标位置信息等 **/ function getSelectText() { var text, userSelection = window.getSelection()||document.selection.createRange();// not IE || ie if (!(text = userSelection.text)) text = userSelection; ret
原文链接:js设置光标插入文字和HTML 在一个textarea的某个光标位置插入文字或者在某个编辑器中插入图片html内容,我最近经常和这些打交道,但总是一团模糊,今天整理一下关于如何插入文字,设置光标,移动光标等操作. 应用场景:新浪微博(网站留言本)中输入框某个位置插入表情.话题或者@ 某个人涉及到插入文本,重新设置光标位置. 代码一:在textarea某个位置插入文本 function insertAtCursor(myField, myValue) { //IE support if