【问题标题】:textarea auto scroll to bottomtextarea 自动滚动到底部
【发布时间】:2016-09-15 23:42:03
【问题描述】:

如果插入了新文本,我正在尝试让我的文本区域滚动到底部。但我就是无法让它工作。

HTML

<textarea id="txt-area" readonly rows="21" cols="48"></textarea>
<button class="buttons" value="1">1</button>
<button class="buttons" value="2">2</button>
<button class="buttons" value="3">3</button>
<button class="buttons" value="4">4</button>
<input type="button" class="buttons" value="Test">
<input type="button" class="Backspace" value="DEL">

JavaScript

var values = [];

$(document).ready(function () {
    $(".buttons").click(function () {
        var cntrl = $(this).html();
        if ($(this)[0].nodeName == "INPUT" )
        {
                cntrl = $(this).attr( "value" );
        }

        $("#txt-area").val(function (_, val){
            return val + cntrl
        });
        values.push($(this).val());
        $("#txt-area").val(values.join("\n"));
    });
            $('.Backspace').on('click', function () {
            values.pop();
            $('#txt-area').val(values.join("\n"));
        });
});

$(document).ready(function(){
    $(".buttons").click(function(){
        $('#txt-area').scrollTop($('#txt-area').scrollHeight);    
    });

});

我的代码也在这个jsFiddle

【问题讨论】:

标签: javascript html button textarea


【解决方案1】:

你需要改变

$('#txt-area').scrollTop($('#txt-area').scrollHeight);    

$('#txt-area').scrollTop($('#txt-area')[0].scrollHeight);    

http://jsfiddle.net/cPYCV/48/

【讨论】:

  • 确实,记录$('#txt-area').scrollHeight 输出undefined
【解决方案2】:

将此添加到您的 JavaScript

window.setInterval(function() {
  var elem = document.getElementById('txt-area');
  elem.scrollTop = elem.scrollHeight;
}, 1000);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-08
    • 1970-01-01
    • 2016-11-13
    • 1970-01-01
    • 2018-08-21
    • 2012-01-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多