说明:直接调用即可
? ? ? ? ? ?el为要获取的文本框的对象,如:$("textarea")
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;
? ?}
? ?)();
}cs