console.info("cursor"); // 为div保存range var _range = null; function displayAreaFocusAndBlur() { debugger // 如果有保存的焦点则重新获取到该焦点,如果没有保存的焦点则以文本最后为该焦点 $('#outCont').on('focus', function (ev) { // $('#displayArea').。Lin var selection = window.getSelection(); if (selection && _range) { selection.removeAllRanges(); selection.addRange(_range); // 添加焦点 } else { var displayArea = document.getElementById('outCont'); // .getElementById('displayArea')。Lin cursorManager.setEndOfContenteditable(displayArea); } }); // 保存焦点到_range $('#outCont').on('blur', function () { // $('#displayArea').。Lin var selection = window.getSelection(); if (selection && selection.rangeCount > 0) { _range = selection.getRangeAt(0).cloneRange(); } }); } //Namespace management idea from http://enterprisejquery.com/2010/10/how-good-c-habits-can-encourage-bad-javascript-habits-part-1/ (function (cursorManager) { //From: http://www.w3.org/TR/html-markup/syntax.html#syntax-elements var voidNodeTags = ['AREA', 'BASE', 'BR', 'COL', 'EMBED', 'HR', 'IMG', 'INPUT', 'KEYGEN', 'LINK', 'MENUITEM', 'META', 'PARAM', 'SOURCE', 'TRACK', 'WBR', 'BASEFONT', 'BGSOUND', 'FRAME', 'ISINDEX']; //From: http://stackoverflow.com/questions/237104/array-containsobj-in-javascript Array.prototype.contains = function (obj) { var i = this.length; while (i--) { if (this[i] === obj) { return true; } } return false; } //Basic idea from: http://stackoverflow.com/questions/19790442/test-if-an-element-can-contain-text function canContainText(node) { if (node.nodeType == 1) { //is an element node return !voidNodeTags.contains(node.nodeName); } else { //is not an element node return false; } }; function getLastChildElement(el) { var lc = el.lastChild; while (lc && lc.nodeType != 1) { if (lc.previousSibling) lc = lc.previousSibling; else break; } return lc; } //Based on Nico Burns's answer cursorManager.setEndOfContenteditable = function (contentEditableElement) { while (getLastChildElement(contentEditableElement) && canContainText(getLastChildElement(contentEditableElement))) { contentEditableElement = getLastChildElement(contentEditableElement); } var range, selection; if (document.createRange) //Firefox, Chrome, Opera, Safari, IE 9+ { range = document.createRange(); //Create a range (a range is a like the selection but invisible) range.selectNodeContents(contentEditableElement); //Select the entire contents of the element with the range range.collapse(false); //collapse the range to the end point. false means collapse to end rather than the start selection = window.getSelection(); //get the selection object (allows you to change selection) selection.removeAllRanges(); //remove any selections already made selection.addRange(range); //make the range you have just created the visible selection } else if (document.selection) //IE 8 and lower { range = document.body.createTextRange(); //Create a range (a range is a like the selection but invisible) range.moveToElementText(contentEditableElement); //Select the entire contents of the element with the range range.collapse(false); //collapse the range to the end point. false means collapse to end rather than the start range.select(); //Select the range (make it the visible selection } }; cursorManager.setEndOfElement = function (element) { if (!element) return; var range, selection; if (document.createRange) //Firefox, Chrome, Opera, Safari, IE 9+ { range = document.createRange(); //Create a range (a range is a like the selection but invisible) range.selectNodeContents(element); //Select the entire contents of the element with the range range.collapse(false); //collapse the range to the end point. false means collapse to end rather than the start selection = window.getSelection(); //get the selection object (allows you to change selection) selection.removeAllRanges(); //remove any selections already made selection.addRange(range); //make the range you have just created the visible selection } else if (document.selection) //IE 8 and lower { range = document.body.createTextRange(); //Create a range (a range is a like the selection but invisible) range.moveToElementText(element); //Select the entire contents of the element with the range range.collapse(false); //collapse the range to the end point. false means collapse to end rather than the start range.select(); //Select the range (make it the visible selection } }; /** * 给元素获取焦点,并将光标设置到上次光标所在位置 * @param {*} element */ cursorManager.focus = function (element) { element.pauseListener = true; element = $(element)[0]; if (document.createRange && element) { element.focus(); if (element.tagName !== "TEXTAREA") try { var range = getRange(); var selection = window.getSelection(); range.setStart(element.rangeContainer.start, element.lastRange.start); range.setEnd(element.rangeContainer.end, element.lastRange.end); selection.removeAllRanges(); selection.addRange(range); var node = element.rangeContainer.start; element.scrollTop = element.scrollInfo.top; element.scrollLeft = element.scrollInfo.left; while (node.nodeType != 1) { node = node.parentNode; } if (node.offsetTop < element.scrollTop || node.offsetTop >= (parseInt(window.getComputedStyle(element).height.replace("px", "")) + element.scrollTop)) element.scrollTop = node.offsetTop - parseInt(window.getComputedStyle(element).height.replace("px", "")) + parseInt(window.getComputedStyle(node).height.replace("ps", "")); } catch (e) { console.error(e) } } element.pauseListener = false; } /** * * @param {*} element */ cursorManager.clearCursorListener = function (element) { element = $(element)[0]; clearInterval(element.linstenerid); element.linstenerid = null; } /** * 保存当前光标所在的元素 * @param {*} element */ cursorManager.setCursorListener = function (element) { element = $(element)[0]; (function (element) { element.linstenerid = setInterval(function () { if (!element.pauseListener && document.hasFocus() && document.activeElement === element) { //不是暂停,也不是失去焦点 if (!element && element.linstenerid) clearInterval(element.linstenerid); var range, selection; if (document.createRange) { range = getRange(); element.lastRange = { start: range.startOffset, end: range.endOffset }; element.rangeContainer = { start: range.startContainer, end: range.endContainer }; element.scrollInfo = { top: element.scrollTop, left: element.scrollLeft }; } } }, 100); })(element) } cursorManager.setEndOfRange = function (range) { if (document.createRange) { range.collapse(false); selection = window.getSelection(); selection.removeAllRanges(); // _range = range.cloneRange(); selection.addRange(range); } }; } (window.cursorManager = window.cursorManager || {}));