简体   繁体   English

使用纯JS和for循环滑动手风琴

[英]Sliding accordion with pure JS and for loop

I've written a for loop, which should run through all accordions and their children, but I can't figure out why it's only working on the first object. 我编写了一个for循环,该循环应遍历所有手风琴及其子级,但是我无法弄清楚为什么它仅在第一个对象上起作用。

Fiddle Example 小提琴的例子

for (
    var i = 0,
    accordion = document.getElementsByClassName('accordion');
    i < accordion.length;
    i++
) {
    var accordion_section = accordion[i].children[i],
        accordion_key = accordion[i].children[i].children[0],
        accordion_bellow = accordion[i].children[i].children[1];

    function accordion_bellow_MarginTop( value ) {
        accordion_bellow.style.marginTop = value + 'px';
    }
    accordion_bellow_MarginTop( -accordion_bellow.offsetHeight );

    accordion_key.onclick = function() {
        if ( accordion_section.getAttribute('class' ) == 'active' ) {
            accordion_section.setAttribute('class', '');
            accordion_bellow_MarginTop( -accordion_bellow.offsetHeight );
        }
        else {
            accordion_section.setAttribute('class', 'active');
            accordion_bellow_MarginTop( 0 );
        }
        return false;
    }
}

There are a couple of issues at play here. 这里有两个问题。 As previous commenters noted, you are not properly looping over each of the sections within your accordion. 正如以前的评论者所指出的,您没有正确地遍历手风琴中的每个部分。 After fixing that, you will also need to address the fact that your onClick handler will not work correctly. 解决此问题后,您还需要解决onClick处理程序将无法正常工作的事实。

The problem with looping over each section is that you are using improper variable scoping. 遍历每个部分的问题是您使用了不正确的变量作用域。 What happens is only the last element you loop over will be affected by the click handler. 只会发生循环的最后一个元素会受到点击处理程序的影响。 This is because the variables "accordion_section" and "accordion_bellow" will always reference the last set of elements in your main for loop. 这是因为变量“ accordion_section”和“ accordion_bellow”将始终引用main for循环中的最后一组元素。

This is contrary to the expectation that they will be the unique element assigned during the loop. 这与它们将成为循环期间分配的唯一元素的预期相反。 The reason for this is because the variables "accordion_section" and "accordion_bellow" are defined outside the scope of the onClick function. 这样做的原因是因为变量“ accordion_section”和“ accordion_bellow”是在onClick函数范围之外定义的。 In order for your onClick to work, those variables need to be defined within a separate scope during each iteration of your for loop. 为了使onClick正常工作,需要在for循环的每次迭代期间在单独的范围内定义这些变量。

In order to do this, you can use an anonymous function like this: 为此,您可以使用如下匿名函数:

for (var i = 0; i < sections.length; i++) 
{
    (function() {
        var section = sections.item(i),
            anchor = sections.item(i).children[0],
            below = sections.item(i).children[1];

        closeBelow(below, -below.offsetHeight);

        anchor.onclick = function () {
            if (section.getAttribute('class' ) == 'active' ) {
                section.setAttribute('class', '');
                closeBelow(below);
            }
            else {
                section.setAttribute('class', 'active');
                openBelow(below);
            }
        }
    })();
}

In this solution, the variables "section", "anchor", and "below" are always specific to the elements you are looping over. 在此解决方案中,变量“ section”,“ anchor”和“ below”始终特定于要循环的元素。 By using a self-executing function, you ensure that each click handler only works with locally scoped variables. 通过使用自执行函数,可以确保每个单击处理程序仅适用于局部范围的变量。

Solution: http://jsfiddle.net/b0u916p4/4/ 解决方案: http//jsfiddle.net/b0u916p4/4/

you need to make another loop inside first 您需要先在内部进行另一个循环

this way you get all accordion and all sections of each 这样,您可以获得所有手风琴以及每个手风琴的所有部分

try this: 尝试这个:

for (i = 0,
     accordion = document.getElementsByClassName('accordion');
    i < accordion.length;
    i++) {

    for (j = 0;
        j <= accordion[i].children.length;
        j++) {


              //your code here

     }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM