Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/459.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 按#id滚动div,我做错了什么?_Php_Javascript - Fatal编程技术网

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

Php 按#id滚动div,我做错了什么?,php,javascript,Php,Javascript,我正在为我的网站设计一个留言板。在数据库中,每条消息都通过自动递增的方式获得一个id。板上的消息位于“li id=#”标记下,“li”从数据库获取id。我试图通过javascript创建滚动功能,这样上下箭头可以在整个消息中移动 到目前为止,我得到的是: down.node.onclick = function() { // down arrow var msgs = document.getElementById("kommentit"); // the <ul> element

我正在为我的网站设计一个留言板。在数据库中,每条消息都通过自动递增的方式获得一个id。板上的消息位于“li id=#”标记下,“li”从数据库获取id。我试图通过javascript创建滚动功能,这样上下箭头可以在整个消息中移动

到目前为止,我得到的是:

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]';
}
down.node.onclick=function(){//向下箭头
var msgs=document.getElementById(“kommentit”);//元素
var a=新数组();
if(msgs.hasChildNodes()){
var children=msgs.childNodes;
对于(变量i=0;i
因此,虽然数组获得了正确的值,但实际的函数不起作用。通过这段代码,我在单击时将…/index.php#[a+1]作为我的url

如果我使用“+a[I+1]+”,页面将刷新整个数组,直到它以#undefined结束

id的格式为“id xx”,这就是问题所在吗?我尝试使用msgs.childNodes[I].id.split(“id-”)拆分id,但如果这样做,我会在数组中得到“xx”作为我的值。

试试看

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];
}
down.node.onclick=function(){//向下箭头
var msgs=document.getElementById(“kommentit”);//元素
var a=新数组();
if(msgs.hasChildNodes()){
var children=msgs.childNodes;
对于(变量i=0;i
如果每次都要滚动到下一个项目,则必须跟踪滚动到的最后一个项目。 不是发明自行车,我建议你使用一些JS框架和插件来滚动。这会节省你很多时间

例如:

  • -框架
  • -滚动插件

为什么要在数组索引中添加1

您是否尝试过:

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

但是我可能会使用jquery(或类似的)来实现类似的功能,因为像上面这样更改url会将大量内容添加到用户的历史记录中,并打断“后退”按钮。

与“+a[I+1]+”一样,它会遍历整个数组,然后以“未定义”来解决问题。(位置会转到最后一个实际值)你能在jsfiddle.net上举个例子吗?你可以在那里找到它,但只有向下箭头是functionalok。我发现了问题。你的for循环是从0到a.length。但是使用
a[i+1]
它是从1到a.length+1,然后你尝试访问一个不存在的值,这就是它未定义的原因。看看我的fixAh解决方案,是的,还是和以前一样,所以它会滚动整个过程。谢谢你的反馈,我想我正在尝试jCarousel之前建议的方法。这里有点新手,这是我的第一次编程所以我一直在学习很多东西。