jQuery:将textarea滚动到给定位置

时间:2011-03-28 11:58:43

标签: javascript jquery textarea scroll

我有一个包含大量文字的文本区域:

<textarea cols="50" rows="10" id="txt">lots and lots of text goes here</textarea>

我想向下滚动textarea,以便用户可以看到第2000个字符。我怎么能用javasctipt / jQuery做到这一点?

$('#txt').scrollToCharNo(2000); // something like this would be great

编辑(我的解决方案)

嗯,我设法使自己的工作。我找到的唯一方法是创建一个与textarea具有相同字体和宽度的DIV,将SPAN放在所需字符附近并找到该跨度的位置。

我确信,有人可能会发现我的解决方案很有用,所以我会将其粘贴到此处:

jQuery.fn.scrollToText = function(search) {
    // getting given textarea contents
    var text = $(this).text();
    // number of charecter we want to show
    var charNo = text.indexOf(search);
    // this SPAN will allow up to determine given charecter position
    var anch = '<span id="anch"></span>';
    // inserting it after the character into the text
    text = text.substring(0, charNo) + anch + text.substring(charNo);

    // creating a DIV that is an exact copy of textarea
    var copyDiv = $('<div></div>')
                    .append(text.replace(/\n/g, '<br />')) // making newlines look the same
                    .css('width', $(this).attr('clientWidth')) // width without scrollbar
                    .css('font-size', $(this).css('font-size'))
                    .css('font-family', $(this).css('font-family'))
                    .css('padding', $(this).css('padding'));

    // inserting new div after textarea - this is needed beacuse .position() wont work on invisible elements
    copyDiv.insertAfter($(this));
    // what is the position on SPAN relative to parent DIV?
    var pos = copyDiv.find('SPAN#anch').offset().top - copyDiv.find('SPAN#anch').closest('DIV').offset().top;
    // the text we are interested in should be at the middle of textearea when scrolling is done
    pos = pos - Math.round($(this).attr('clientHeight') / 2);
    // now, we know where to scroll!
    $(this).scrollTop(pos);
    // no need for DIV anymore
    copyDiv.remove();
};


$(function (){
    $('#scroll_button').click(
        function() {
            // scrolling to "FIND ME" using function written above
            $('#txt').scrollToText('FIND ME');
            return false;
        }
    );
});

这是一个演示(它有效!):http://jsfiddle.net/KrVJP/

由于没有一个答案真正解决问题,我会接受experimentX的答案:谢谢你付出努力帮助我,我很感激!

2 个答案:

答案 0 :(得分:3)

我不确定它是否有用。另请检查here。它似乎在第2000,第1500和第1000位工作。

编辑混淆了font-size或line-height ???

 $(function (){
    var height = 2000/$('#txt').attr('cols');

    $('#txt').scrollTop(height*13);

    $('#txt').selectRange(2000,2000); //this is just to check
});

$.fn.selectRange = function(start, end) { //this is just to check 
    return this.each(function() {
        if (this.setSelectionRange) {
            this.focus();
            this.setSelectionRange(start, end);
        } else if (this.createTextRange) {
            var range = this.createTextRange();
            range.collapse(true);
            range.moveEnd('character', end);
            range.moveStart('character', start);
            range.select();
        }
    });
};

更新这个怎么样

    var height = 1200/$('#txt').attr('cols');
    var line_ht = $('#txt').css('line-height');
    line_ht = line_ht.replace('px','');

    height = height*line_ht; 

答案 1 :(得分:0)

您可以使用this jquery插件