javascript 如何滚动到 ul 的底部?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15757144/
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
How to scroll to bottom of the ul?
提问by SuReSh PaTi
I have developed one application using PhoneGap. In my application I have displayed a number of elements in a listview using ui
li
. Here I want to scroll to the last element in list. For that i have used following code,
我使用 PhoneGap 开发了一个应用程序。在我的应用程序中,我使用ui
li
. 在这里,我想滚动到列表中的最后一个元素。为此,我使用了以下代码,
$('#dates li').last().addClass('active-li').focus();
$('#dates li').last().focus();
I have used this one also,
我也用过这个,
$('#dates').scrollTop($('#dates li:nth-child(14)').position().top);;
extra class is adding but focus is not working.
额外的课程正在添加,但焦点不起作用。
回答by u283863
Add the tabindex
attribute:
添加tabindex
属性:
<li tabindex="1"></li>
http://jsfiddle.net/DerekL/GEsmb/
http://jsfiddle.net/DerekL/GEsmb/
Because you can not focus on li
. It's not a text field or button or something like that.
因为你无法专注li
。它不是文本字段或按钮或类似的东西。
回答by Praveen Kumar Purushothaman
You can use this code:
您可以使用此代码:
$(document).ready(function(){
$('body').scrollTop($('ul li').last().position().top + $('ul li').last().height());
});