我在使用Javascript从内容可编辑div的开头获取插入位置时遇到问题。我能够从行的开始 - 但不是从div的开头得到偏移量。从div的开头偏移是我想要的。例如:
This is line number one.
This []is line number two. <-- [] Denotes where the caret position is
当试图从div的开始 - 找到插入符号位置时 - 当我真正想要30时,我得到值6。 这是我正在使用的代码:
function updateCursorPosition() {
cursorPosition = window.getSelection().baseOffset;
alert(cursorPosition);
}
使用onclick事件触发此函数。如前所述,我只是从行的开头得到偏移而不是div的开头。感谢先进的人提供您的意见。
我要找的偏移量是字符,而不是像素。
答案 0 :(得分:2)
尝试以下功能:
关于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;
};
以下功能可能有所帮助。它将从textarea的开头为您提供开始和结束选定的字符。如果没有选择,它将为您提供插入符号的字符位置:
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;