Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/369.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript:如何使用多行从内容可编辑div的开头获取插入符号偏移量_Javascript_Dom_Getselection - Fatal编程技术网

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

Javascript:如何使用多行从内容可编辑div的开头获取插入符号偏移量,javascript,dom,getselection,Javascript,Dom,Getselection,我在使用Javascript从内容可编辑div的开头获取插入符号位置时遇到问题。我可以得到从行首开始的偏移量,但不能得到从div开始的偏移量。从div开始的偏移量就是我想要的。例如: This is line number one. This []is line number two. <-- [] Denotes where the caret position is 此函数由onclick事件触发。如前所述,我只得到从行开始的偏移量,而不是div的开始。感谢advanced中的任何

我在使用Javascript从内容可编辑div的开头获取插入符号位置时遇到问题。我可以得到从行首开始的偏移量,但不能得到从div开始的偏移量。从div开始的偏移量就是我想要的。例如:

This is line number one.
This []is line number two.  <-- [] Denotes where the caret position is
此函数由onclick事件触发。如前所述,我只得到从行开始的偏移量,而不是div的开始。感谢advanced中的任何人的输入


我要查找的偏移量是字符,而不是像素。

请尝试以下功能:

JSFIDLE上的示例:

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

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;

这不是一件容易的事,你唯一的希望就是在互联网上找到一些东西,但由于互联网是一个黑洞,这将是很难的。谢谢雅各布你的草率回答。我更新了我的问题,注意到我要查找的是字符偏移,而不是像素偏移。@Korupshen使用另一个函数更新了我的答案,该函数可能有助于您尝试执行的操作:)@JacobCarablo是的,这正是我要查找的。谢谢你的帮助。
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;