【发布时间】:2013-02-15 22:28:46
【问题描述】:
我即将在我的 contenteditable div 中集成 Facebook位置。
我需要知道如何在 IE 和其他浏览器的 JavaScript 中找出插入符号位置之前的最后一个字符。我可以访问 Jquery 库。
(function($) {
$.fn.getCursorPosition = function() {
var input = this.get(0);
if (!input) return; // No (input) element found
if ('selectionStart' in input) {
// Standard-compliant browsers
return input.selectionStart;
} else if (document.selection) {
// IE
input.focus();
var sel = document.selection.createRange();
var selLen = document.selection.createRange().text.length;
sel.moveStart('character', -input.value.length);
return sel.text.length - selLen;
}
}
})(jQuery);
eg.
var caretPosition = $("#contenteditablediv").getCursorPosition();
var lastchar = getchar(caretposition -1);???
【问题讨论】:
-
该功能不适用于非 IE 浏览器中的 contenteditable 元素。它用于输入和文本区域。
-
是的,我知道这只是一个例子。你能建议我用其他方法找到插入符号位置之前的最后一个字符吗??
标签: javascript jquery autocomplete contenteditable caret