Commit 3056d5c1 authored by 黄奎's avatar 黄奎

页面修改

parent 207aaf55
...@@ -21,6 +21,7 @@ ...@@ -21,6 +21,7 @@
}, },
methods: { methods: {
changeText: function () { changeText: function () {
var that = this;
let sel = window.getSelection(); let sel = window.getSelection();
let offset = sel.anchorOffset; let offset = sel.anchorOffset;
if (this.textLength && this.$el.innerText.length > this.textLength) { if (this.textLength && this.$el.innerText.length > this.textLength) {
...@@ -32,16 +33,50 @@ ...@@ -32,16 +33,50 @@
var range = sel.getRangeAt(0); var range = sel.getRangeAt(0);
var textNode = range.startContainer; var textNode = range.startContainer;
var textNodeName = textNode.nodeName; var textNodeName = textNode.nodeName;
console.log("textNode", textNode.nodeName);
if (textNodeName == "#text") { if (textNodeName == "#text") {
range.setStart(textNode, offset); range.setStart(textNode, offset);
sel.removeAllRanges(); sel.removeAllRanges();
sel.addRange(range); sel.addRange(range);
} else {
that.setCaretPosition(that.$el, offset);
} }
} catch (err) { } catch (err) {
console.log(err); console.log(err);
} }
}, 0); }, 0);
},
//重置div光标位置
setCaretPosition(element, offset) {
var range = document.createRange();
var sel = window.getSelection();
//select appropriate node
var currentNode = null;
var previousNode = null;
for (var i = 0; i < element.childNodes.length; i++) {
//save previous node
previousNode = currentNode;
//get current node
currentNode = element.childNodes[i];
//if we get span or something else then we should get child node
while (currentNode.childNodes.length > 0) {
currentNode = currentNode.childNodes[0];
}
//calc offset in current node
if (previousNode != null) {
offset -= previousNode.length;
}
//check whether current node has enough length
if (offset <= currentNode.length) {
break;
}
}
//move caret to specified offset
if (currentNode != null) {
range.setStart(currentNode, offset);
range.collapse(true);
sel.removeAllRanges();
sel.addRange(range);
}
} }
} }
}; };
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment