从光标位置获取 contentEditable Div 中键入的单词

发布于 2024-11-26 22:28:38 字数 1321 浏览 3 评论 0原文

我一直在不停地阅读,试图完成这件事,并找到了许多接近的答案,但没有雪茄。这是我的场景:

我正在尝试编写一个 jQuery 插件,使其像智能感知编辑器一样为我的用户提供关键字、数据类型和可能的方法。这感觉就像 IDE 中的 codeComplete。

我需要获取输入的部分单词,以便在关键字数组中进行查找并找到匹配项以填充到下拉列表中。在我发现一些错误之前,一切都运行得很好。

我有一个 contentEditable DIV,需要获取光标位置。我在我之前提到的一个答案中慷慨地提供了一些代码:

function getCursorPos() {
var cursorPos = 0;
if (window.getSelection) {
    var selObj = window.getSelection();
    var selRange = selObj.getRangeAt(0);
    cursorPos = findNode(selObj.anchorNode.parentNode.childNodes, selObj.anchorNode) + selObj.anchorOffset; /* FIXME the following works wrong in Opera when the document is longer than 32767 chars */
    //alert(cursorPos);
}
else if (document.selection) {
    var range = document.selection.createRange();
    var bookmark = range.getBookmark(); /* FIXME the following works wrong when the document is longer than 65535 chars */
    cursorPos = bookmark.charCodeAt(2) - 11; /* Undocumented function [3] */
    //alert(cursorPos);
}
return cursorPos; }

function findNode(list, node) {
for (var i = 0; i < list.length; i++) {
    if (list[i] == node) {
        return i;
    }
}
return -1; }

我看到的问题是范围+偏移量适用于大约 7-8 个字符,然后空格和其他字符似乎停止光标“前进”,然后我的整个 getWord() 函数都被抛弃了。

有人可以帮我解释一下吗?也许我没有使用对我的具体情况有帮助的代码。我花了几天时间试图理解 Range 和 SelectionRange 对象,这让我发疯。 :)

I have been reading nonstop on trying to get this done and found many answers that are close but no cigar. Here is my scenario:

I am trying to write a jQuery plugin to work like an intellisense editor for my users with keywords and datatypes and possible methods. This will feel like codeComplete in IDEs.

I need to get the partial word that is input so as to do a lookup in my keyword array and find matches to populate in a dropdown. Things work pretty well up until I found a few bugs.

I have a contentEditable DIV and need to get the cursor position. I have some code that was graciously provided in one of the previous answers I spoke of:

function getCursorPos() {
var cursorPos = 0;
if (window.getSelection) {
    var selObj = window.getSelection();
    var selRange = selObj.getRangeAt(0);
    cursorPos = findNode(selObj.anchorNode.parentNode.childNodes, selObj.anchorNode) + selObj.anchorOffset; /* FIXME the following works wrong in Opera when the document is longer than 32767 chars */
    //alert(cursorPos);
}
else if (document.selection) {
    var range = document.selection.createRange();
    var bookmark = range.getBookmark(); /* FIXME the following works wrong when the document is longer than 65535 chars */
    cursorPos = bookmark.charCodeAt(2) - 11; /* Undocumented function [3] */
    //alert(cursorPos);
}
return cursorPos; }

function findNode(list, node) {
for (var i = 0; i < list.length; i++) {
    if (list[i] == node) {
        return i;
    }
}
return -1; }

The issue I am seeing is that the range + offset work for about 7-8 characters then spaces and other characters seems to stop cursor "progression" and then my whole getWord() function is thrown off.

Does anyone have any light to shed on this for me? Maybe I am not using code that helps in my specific situation. I have spent days now trying to understand Range and selectionRange objects and it is driving me nuts. :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文