最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Set focus to the end of table inside of div when adding a new row - Stack Overflow

programmeradmin3浏览0评论

I have a table within a div with a set height, when the table is bigger than the div the scroll-y bar appear. The problem is that if the scroll bar of the div is at the top, leaving other row at the bottom hidden, and the user press the button to add a new row, this row is added at the bottom of the table, therefore you can't see it.

Is there anyway with javascript, vbscript, or css to set focus to the row it's been created so it is visible when added?

This is the code where the function to add a new row would be called.

   strRow = strRow + "<div>
        <input ID='AddRowTableNew_" + cstr(newrow.id) + "' type='button' 
        value='Add New Row' 
        onclick='vbscript:AddNewRow 0," + cstr(newRow.id)+ "'/>
    </div>"

Any help would be appreciated.

PD: I would need this to be on VBScript or JavaScript, No JQuery please.

Thanks.

I have a table within a div with a set height, when the table is bigger than the div the scroll-y bar appear. The problem is that if the scroll bar of the div is at the top, leaving other row at the bottom hidden, and the user press the button to add a new row, this row is added at the bottom of the table, therefore you can't see it.

Is there anyway with javascript, vbscript, or css to set focus to the row it's been created so it is visible when added?

This is the code where the function to add a new row would be called.

   strRow = strRow + "<div>
        <input ID='AddRowTableNew_" + cstr(newrow.id) + "' type='button' 
        value='Add New Row' 
        onclick='vbscript:AddNewRow 0," + cstr(newRow.id)+ "'/>
    </div>"

Any help would be appreciated.

PD: I would need this to be on VBScript or JavaScript, No JQuery please.

Thanks.

Share Improve this question edited Feb 3, 2010 at 11:17 Amra asked Feb 3, 2010 at 10:37 AmraAmra 25.3k29 gold badges85 silver badges93 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 5

Try Element.scrollIntoView();. It works in Firefox, IE6+, Opera and Google Chrome/Safari.

You can also provide a boolean to the function which will specify where to scroll the element to:

// Align the element with the top of the parent
Element.scrollIntoView();
Element.scrollIntoView(true);

// Align the element with the bottom of the parent
Element.scrollIntoView(false);

EDIT: MDC documentation for scrollIntoView with an example

To scroll to the bottom, you could also just set the element's scrollTop property to equal the scrollHeight property:

Element.scrollTop = Element.scrollHeight;

Try this ...

var div = document.getElementById('the_div');
div.scrollTop = div.scrollHeight;

Hope it helps.

发布评论

评论列表(0)

  1. 暂无评论