douhui8025 2011-07-20 11:29
浏览 31
已采纳

用#id滚动div,我做错了什么?

I'm programming a message board for my site. Every message gets an id by auto increment in the database. The messages on the board are under "li id=#" tags where the "li" gets the id from the database. I've tried to create scrolling function via javascript so that down and up arrows would move across whole messages.

This is what I've got so far:

down.node.onclick = function() { // down arrow

var msgs = document.getElementById("kommentit"); // the <ul> element
var a = new Array();


if (msgs.hasChildNodes()) {
    var children = msgs.childNodes;
    for (var i = 0; i < children.length; i++) {
        if (msgs.childNodes[i].tagName == "LI") {
            a.push(msgs.childNodes[i].id); // array with the id's
        }
    }

}

for (var i = 0; i < a.length; i++) { // this is what goes wrong
    parent.location.href = '#' + 'a[i + 1]';
}

So while the array gets the right values the actual function doesn't work. With this code I get .../index.php#[a + 1] as my url when I click.

If I use ''+a[i + 1]+'' the page refreshes through the whole array until it settles with #undefined.

The id's are in format "id-xx" so is that the problem? I tried splitting the id by using msgs.childNodes[i].id.split("id-") but if I do this I get ",xx" as my values in the array.

  • 写回答

3条回答 默认 最新

  • douzhang8144 2011-07-20 11:44
    关注

    If you want scroll to next item each time you have to track down the last item you scrolled to. Not to invent the bicycle, I suggest you use some JS frameworks and plugins for scrolling. It will save you a lot of time.

    Examples are:

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 Macbookpro 连接热点正常上网,连接不了Wi-Fi。
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果