scrollTop()不能与IE上的TextArea一起工作

scrollTop() doesn't work with TextArea on IE

本文关键字:上的 TextArea 一起 工作 IE 不能 scrollTop      更新时间:2023-09-26

这个问题很常见,但是我还没有见过类似的问题。我有我的。cshtml文件,我已经声明了一个TextArea使用微软MVC htmlhelper类。代码如下:

@Html.TextArea("contentContainer", new Dictionary<string, object> { { "style", "width: 100%; height: 600px" } })

然后我有一个函数,它一直从LogFile中读取,并在每次有更新时附加/添加此文件的内容到此TextArea。我在jQuery中有一个函数它将内容附加到TextArea然后我想让它滚动TextArea到TextArea的最底部。代码很简单:

appendContent: function (content) {
    var $textArea = $('#contentContainer');
    var shouldBeScrolled = true;
    $textArea.append(content);
    var shouldBeScrolled = true;
    if (shouldBeScrolled) {
        $textArea.scrollTop(
           $textArea[0].scrollHeight
        );
    }
}

请忽略shouldbesrolled变量。我只是有它,因为以后我打算对滚动创建一些约束。

OK,基本上这段代码在Chromium上工作完美,内容被附加,TextArea滚动到底部。在ie浏览器上,它根本不滚动。为什么会发生这种情况?

这很可能是JQuery的一个bug -我之前有过同样的问题,作为一个解决方案,我最终滚动到包含我的TextArea的另一个项目,并且它的顶部边界与TextArea一致。

像这样:

<div id="TextAreaContainer">
  <textarea></textarea>
</div>

然后Javascript看起来像:

$("#TextAreaContainer").scrollTop();