2

<body>を持つiframe 内のキャレット位置を取得しようとしていcontentEditable = trueます。クロムアドオンからコンテンツスクリプトを実行し、次のようにアクションをバインドしています:

getCaretPosition = function(element) {
    var range = parent.$("iframe.html").getCursorPosition();
    alert(range);
    return caretOffset;
}

getCursorPosition同様のスタックオーバーフローの回答で、次のコードを見つけました。

(function ($, undefined) {
$.fn.getCursorPosition = function() {
    var el = $(this).get(0);
    var pos = 0;
    if('selectionStart' in el) {
        pos = el.selectionStart;
    } else if('selection' in document) {
        el.focus();
        var Sel = document.selection.createRange();
        var SelLength = document.selection.createRange().text.length;
        Sel.moveStart('character', -el.value.length);
        pos = Sel.text.length - SelLength;
    }
    return pos;
}
})(jQuery);

しかし、常に0の位置を返しています。

4

0 に答える 0