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

javascript - How do I use jQueryJS to scroll a listbox to the top - Stack Overflow

programmeradmin5浏览0评论

JSFiddle

In the fiddle example I have several attempts to scroll a listbox to the top (or any point in the list) after programmatically selecting an item. I have not been able to find a way that works or an example on SO that shows how to successfully do this. Below is the code or you can see it on JSFiddle. Can someone point me in the right direction please.

<select id='focusScrollTest' multiple='multiple'>
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
    <option value="5">5</option>
    <option value="6">6</option>
    <option value="7">7</option>
    <option value="8">8</option>
    <option value="9">9</option>
</select>

$(document).ready(function(){
//return the current scroll position before selecting the last option
var x = $("#focusScrollTest").scrollTop();

//Displays 0
//alert(x);

//Select the last option value
$("#focusScrollTest option[value='9']").attr('selected', 'selected');

//return the current scroll position after selecting the last option
var y = $("#focusScrollTest").scrollTop();

//Displays 85
//alert(y);

///
///   These do not change the scroll
///
//$("#focusScrollTest option[value='1']").scrollTo();
//$("#focusScrollTest option[value='1']").scrollTo(0);
//$("#focusScrollTest").scrollTo(x);
//$("#focusScrollTest").scrollTo(0);
//$("#focusScrollTest").scrollTo(1);
//$("#focusScrollTest").scrollTop = 0;
//$("#focusScrollTest option[value='1']").scrollTop = 0;
//$("#focusScrollTest").focus();
//$("#focusScrollTest option[value='1']").focus();
//$("#focusScrollTest").blur().focus();
//$("#focusScrollTest option[value='1']").blur().focus();

//This is very interesting, even though it is done last the box
//still scrolled to the end
//$("#focusScrollTest option[value='1']").attr('selected', 'selected');

});

JSFiddle

In the fiddle example I have several attempts to scroll a listbox to the top (or any point in the list) after programmatically selecting an item. I have not been able to find a way that works or an example on SO that shows how to successfully do this. Below is the code or you can see it on JSFiddle. Can someone point me in the right direction please.

<select id='focusScrollTest' multiple='multiple'>
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
    <option value="5">5</option>
    <option value="6">6</option>
    <option value="7">7</option>
    <option value="8">8</option>
    <option value="9">9</option>
</select>

$(document).ready(function(){
//return the current scroll position before selecting the last option
var x = $("#focusScrollTest").scrollTop();

//Displays 0
//alert(x);

//Select the last option value
$("#focusScrollTest option[value='9']").attr('selected', 'selected');

//return the current scroll position after selecting the last option
var y = $("#focusScrollTest").scrollTop();

//Displays 85
//alert(y);

///
///   These do not change the scroll
///
//$("#focusScrollTest option[value='1']").scrollTo();
//$("#focusScrollTest option[value='1']").scrollTo(0);
//$("#focusScrollTest").scrollTo(x);
//$("#focusScrollTest").scrollTo(0);
//$("#focusScrollTest").scrollTo(1);
//$("#focusScrollTest").scrollTop = 0;
//$("#focusScrollTest option[value='1']").scrollTop = 0;
//$("#focusScrollTest").focus();
//$("#focusScrollTest option[value='1']").focus();
//$("#focusScrollTest").blur().focus();
//$("#focusScrollTest option[value='1']").blur().focus();

//This is very interesting, even though it is done last the box
//still scrolled to the end
//$("#focusScrollTest option[value='1']").attr('selected', 'selected');

});

Share Improve this question asked Oct 21, 2013 at 17:32 JabberwockyDepilerJabberwockyDepiler 3,4002 gold badges43 silver badges54 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 9

Try this:

http://jsfiddle/jLzpU/

$('#focusScrollTest').scrollTop(0); 
发布评论

评论列表(0)

  1. 暂无评论