倚天杖
//获取选择域位置,如果未选择便是光标位置function getSelection(el) { return ( ('selectionStart' in el && function () { var l = el.selectionEnd - el.selectionStart; return { start: el.selectionStart, end: el.selectionEnd, length: l, text: el.value.substr(el.selectionStart, l) }; }) || (document.selection && function () { el.focus(); var r = document.selection.createRange(); if (r === null) { return { start: 0, end: el.value.length, length: 0 } } var re = el.createTextRange(); var rc = re.duplicate(); re.moveToBookmark(r.getBookmark()); rc.setEndPoint('EndToStart', re); return { start: rc.text.length, end: rc.text.length + r.text.length, length: r.text.length, text: r.text }; }) || function () { return null; } )();}//替换选择function replaceSelection(el) { var text = arguments[0] || ''; return ( /* mozilla / dom 3.0 */ ('selectionStart' in el && function () { el.value = el.value.substr(0, el.selectionStart) + text + el.value.substr(el.selectionEnd, el.value.length); return this; }) || /* exploder */ (document.selection && function () { el.focus(); document.selection.createRange().text = text; return this; }) || /* browser not supported */function () { el.value += text; return jQuery(el); } )();}