为什么这个 div 不会自动滚动?

发布于 2024-10-24 02:08:25 字数 1735 浏览 3 评论 0原文

我已经让它工作得很好,但自动滚动拒绝工作......知道为什么吗?

PS 我只想使用 jQuery...commentsList 位于 commentsPanel

    $.ajax({
            url: 'comments.php',
            type: 'POST',
            data: data,
            cache: false,
            success: function (comments_html) {

                $('#commentsPanel').html(comments_html);
                var commentsList = document.getElementById('#commentsList');
                commentsList.scrollTop = commentsList.scrollHeight;
                $('#loading').hide();
            }
        });

谢谢!

   <div id="commentsPanel">
    <table width="260" height="220" border="0" cellpadding="3">
                    <tr><td height="5"></td><td><span style="text-align:right;"><a href="#close" rel="close_comment">Close</tr>
                    <tr><td height="220" valign="top">
                        <div id="commentsList" style="overflow: auto; width: 260px; height: 220px; text-align=left">
    <!-- CONTENT -->

    </div>
                </td></tr>
                <tr><td height="50">
                <form id="new_comment" name="comment_form" method="post" action="comments.php">
                    <input type="hidden" id="trackID" value="' . $track . '">
                    <input type="text" size="25" id="new_comment_text" /><span style="text-align:right">
                    <input type="submit" value="Comment" id="submit_comment"/></span>
                </form>
                </td></tr>
                </table>
</div>

I've got this working nicely, but the autoscroll refuses to work... any idea why?

PS I only want to use jQuery... commentsList is inside of commentsPanel

    $.ajax({
            url: 'comments.php',
            type: 'POST',
            data: data,
            cache: false,
            success: function (comments_html) {

                $('#commentsPanel').html(comments_html);
                var commentsList = document.getElementById('#commentsList');
                commentsList.scrollTop = commentsList.scrollHeight;
                $('#loading').hide();
            }
        });

Thanks!

   <div id="commentsPanel">
    <table width="260" height="220" border="0" cellpadding="3">
                    <tr><td height="5"></td><td><span style="text-align:right;"><a href="#close" rel="close_comment">Close</tr>
                    <tr><td height="220" valign="top">
                        <div id="commentsList" style="overflow: auto; width: 260px; height: 220px; text-align=left">
    <!-- CONTENT -->

    </div>
                </td></tr>
                <tr><td height="50">
                <form id="new_comment" name="comment_form" method="post" action="comments.php">
                    <input type="hidden" id="trackID" value="' . $track . '">
                    <input type="text" size="25" id="new_comment_text" /><span style="text-align:right">
                    <input type="submit" value="Comment" id="submit_comment"/></span>
                </form>
                </td></tr>
                </table>
</div>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

硬不硬你别怂 2024-10-31 02:08:25

使用 var commentsList = document.getElementById('#commentsList'); 时,不需要包含 #

var commentsList = document.getElementById('commentsList');

When using var commentsList = document.getElementById('#commentsList'); you dont need to include the #

var commentsList = document.getElementById('commentsList');
心在旅行 2024-10-31 02:08:25

有趣的是,您说:“我只想使用 jQuery”,但您却使用了 document.getElementById()。

您将 jQuery 与 DOM 方法混淆了。将此行:更改

var commentsList = document.getElementById('#commentsList');

为:

var commentsList = $('#commentsList');

Funny, you said: "I only want to use jQuery" and yet you use document.getElementById().

You're mixing up jQuery with DOM methods. Change this line:

var commentsList = document.getElementById('#commentsList');

to this:

var commentsList = $('#commentsList');
夏尔 2024-10-31 02:08:25
$.ajax({
            url: 'comments.php',
            type: 'POST',
            data: data,
            cache: false,
            success: function (comments_html) {

                $('#commentsPanel').html(comments_html);
                var commentsList = $('#commentsList'); //<- changed
                commentsList.scrollTop(commentsList.scrollHeight()); //<- add brackets here
                $('#loading').hide();
            }
        });
$.ajax({
            url: 'comments.php',
            type: 'POST',
            data: data,
            cache: false,
            success: function (comments_html) {

                $('#commentsPanel').html(comments_html);
                var commentsList = $('#commentsList'); //<- changed
                commentsList.scrollTop(commentsList.scrollHeight()); //<- add brackets here
                $('#loading').hide();
            }
        });
听风念你 2024-10-31 02:08:25

您说 commentsList 位于 commentsPanel,但您正在用这一行替换 commentsPanel 内的所有内容:

$('#commentsPanel').html(comments_html);

您确定吗commentsList 当时确实存在吗?在 var commentsList = ... 行后添加以下内容。

console.log(commentsList);

以确保它确实存在。

You say that commentsList is inside commentsPanel but you are replacing everything inside commentsPanel with this line:

$('#commentsPanel').html(comments_html);

Are you sure that commentsList actually exists at the time? Add the following after your var commentsList = ... line.

console.log(commentsList);

To make sure that it actually exists.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文