可滚动<div> </div>中的焦点元素

时间:2013-02-04 02:08:53

标签: jquery scroll

        var newrow =
     $(
     "<tr id=" + mahs + ">"
    + "<td id=\"stt\">" + $("#txtIncrement").val() + "</td>"
    + "<td id=\"mahs\">" + mahs + "</td>"
    + "<td id=\"fullname\">" + $("#txtFullname").val() + "</td>"
    + "<td id=\"delete\">" + "<a href=\"#\"><img src=\"/Contents/Images/delete.png\" style=\"width:30px;height:30px;\" alt=\"\" /></a>" + "</td>"
    + "</tr>");
    $("#tbContent").append(newrow);

我使用此代码在下面的div标签中向表(tbContent)添加新行。

 <div  id="scrollpanel" class="cvleft" style="height:417px; overflow:scroll;">
        <table id="tbContent">
        <thead>
            <tr>
                <th>STT</th>
                <th>HS</th>
                <th>Name</th>
                <th>Delete</th>

            </tr>
        </thead>
        <tbody>
        </tbody>
        </table>
    </div>

如果将该行添加到表格中,该如何关注该行? 谢谢!

2 个答案:

答案 0 :(得分:1)

假设您在输入新行后尝试滚动到div的底部,请尝试以下操作:

var height = $('#scrollpanel')[0].scrollHeight;
$('#scrollpanel').scrollTop(height);

或者如果你想让它动画:

var height = $('#scrollpanel')[0].scrollHeight;
$("#scrollpanel").animate({ scrollTop: height}, 1000);
祝你好运。

答案 1 :(得分:0)

如何尝试 - 在添加行时使用jquery可能像 - &gt;

$('#scrollpanel').scrollIntoView() 

How can I focus on a given table's row using javascript/jquery?