gpt4 book ai didi

Javascript:如何从多行内容可编辑div的开头获取插入符偏移量

转载 作者:行者123 更新时间:2023-11-30 17:29:15 24 4
gpt4 key购买 nike

我在使用 Javascript 从内容可编辑 div 的开头获取插入符号位置时遇到问题。我能够从 --beginning of the line-- 但不是从 div 的开头获得偏移量。从 div 开始的偏移量是我想要的。例如:

This is line number one.
This []is line number two. <-- [] Denotes where the caret position is

当试图从 div 的 --beginning 找到插入符号位置时,我得到的值是 6,而我真的想要 30。这是我正在使用的代码:

function updateCursorPosition() {
cursorPosition = window.getSelection().baseOffset;
alert(cursorPosition);
}

此函数由 onclick 事件触发。如前所述,我只是从行的开头而不是 div 的开头获取偏移量。提前感谢任何人的输入。

我正在寻找的偏移量以字符为单位,而不是以像素为单位。

最佳答案

试试下面的函数:

JSFiddle 上的示例:http://jsfiddle.net/9ThVr/

var getCaretPixelPos = function ($node, offsetx, offsety){
offsetx = offsetx || 0;
offsety = offsety || 0;

var nodeLeft = 0,
nodeTop = 0;
if ($node){
nodeLeft = $node.offsetLeft;
nodeTop = $node.offsetTop;
}

var pos = {left: 0, top: 0};

if (document.selection){
var range = document.selection.createRange();
pos.left = range.offsetLeft + offsetx - nodeLeft + 'px';
pos.top = range.offsetTop + offsety - nodeTop + 'px';
}else if (window.getSelection){
var sel = window.getSelection();
var range = sel.getRangeAt(0).cloneRange();
try{
range.setStart(range.startContainer, range.startOffset-1);
}catch(e){}
var rect = range.getBoundingClientRect();
if (range.endOffset == 0 || range.toString() === ''){
// first char of line
if (range.startContainer == $node){
// empty div
if (range.endOffset == 0){
pos.top = '0px';
pos.left = '0px';
}else{
// firefox need this
var range2 = range.cloneRange();
range2.setStart(range2.startContainer, 0);
var rect2 = range2.getBoundingClientRect();
pos.left = rect2.left + offsetx - nodeLeft + 'px';
pos.top = rect2.top + rect2.height + offsety - nodeTop + 'px';
}
}else{
pos.top = range.startContainer.offsetTop+'px';
pos.left = range.startContainer.offsetLeft+'px';
}
}else{
pos.left = rect.left + rect.width + offsetx - nodeLeft + 'px';
pos.top = rect.top + offsety - nodeTop + 'px';
}
}
return pos;
};

以下函数可能会有所帮助。它将为您提供从文本区域开头开始和结束的选定字符。如果没有选择,它会给你插入符号的字符位置:

function getInputSelection(el) {
var start = 0, end = 0, normalizedValue, range,
textInputRange, len, endRange;

if (typeof el.selectionStart == "number" && typeof el.selectionEnd == "number") {
start = el.selectionStart;
end = el.selectionEnd;
} else {
range = document.selection.createRange();

if (range && range.parentElement() == el) {
len = el.value.length;
normalizedValue = el.value.replace(/\r\n/g, "\n");

// Create a working TextRange that lives only in the input
textInputRange = el.createTextRange();
textInputRange.moveToBookmark(range.getBookmark());

// Check if the start and end of the selection are at the very end
// of the input, since moveStart/moveEnd doesn't return what we want
// in those cases
endRange = el.createTextRange();
endRange.collapse(false);

if (textInputRange.compareEndPoints("StartToEnd", endRange) > -1) {
start = end = len;
} else {
start = -textInputRange.moveStart("character", -len);
start += normalizedValue.slice(0, start).split("\n").length - 1;

if (textInputRange.compareEndPoints("EndToEnd", endRange) > -1) {
end = len;
} else {
end = -textInputRange.moveEnd("character", -len);
end += normalizedValue.slice(0, end).split("\n").length - 1;
}
}
}
}

return {
start: start,
end: end
};
}

使用示例:

var selection = getInputSelection(textarea);
var start = selection.start;
var end = selection.end;

关于Javascript:如何从多行内容可编辑div的开头获取插入符偏移量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23526667/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com