Javascript 使用 jQuery 在 textarea 中向下滚动

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8467368/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 06:07:03  来源:igfitidea点击:

Scroll to down in textarea with jQuery

javascriptjqueryhtml

提问by Roy Genergon

<h2>Greetings</h2>
<div class="container">
  <div class="inner">
    Hello
    <p>Test</p>
  </div>
  <textarea id="one" class="inner">
    Goodbye
</textarea>
</div>

$("#one").append("your text to append");
$("#one").append("your text to append");
$("#one").append("your text to append");
$("#one").append("your text to append");

LIVE: http://jsfiddle.net/tGFmq/

现场直播:http: //jsfiddle.net/tGFmq/

how can i make automatically scroll to down in this textarea?

如何在此文本区域中自动向下滚动?

回答by Romanulus

Add this bit to your code (preferably at the end of whatever inserts you have):

将此位添加到您的代码中(最好在您拥有的任何插入的末尾):

    var psconsole = $('#one');
    if(psconsole.length)
       psconsole.scrollTop(psconsole[0].scrollHeight - psconsole.height());

回答by u283863

See this Live Demo: here

看到这个现场演示: here

To calculate the bottom scrollTop, you can simply subtractthe heightfrom the scrollHeight:

为了计算底部scrollTop,你可以简单地减去height来自scrollHeight

var oneDiv = $("#one");
bottom = oneDiv.prop('scrollHeight') - oneDiv.height()

Then you can set its scrollTop to bottom, or use amazing jQuery's animate()for cool animation.

然后你可以将它的 scrollTop 设置为bottom,或者使用惊人的jQueryanimate()来制作很酷的动画。

Live Demo: here

现场演示: here

回答by Mitchell Reynolds

I realised my problem was that I had the code in the incorrect place. -> Placed under the element and got the problem to solve (rookie mistake....) -- Just a reminder to all.

我意识到我的问题是代码在错误的地方。-> 放置在元素下并解决问题(菜鸟错误....) - 只是提醒所有人。