简体   繁体   English

简单的JavaScript手风琴-如何获取被点击面板的索引?

[英]Simple JavaScript accordion - how to get the index of the clicked panel?

I created a simple accordion with plain JavaScript, no jQuery. 我用普通的JavaScript(没有jQuery)创建了一个简单的手风琴。 It dynamically is created by the number of data given in an external JavaScript file in JSON format. 它是由外部JavaScript文件以JSON格式给出的数据数量动态创建的。

The accordion looks like that in the html code: 手风琴在html代码中看起来像这样:

<div id="description"> 
<script language="javascript" type="text/javascript">
startAccordion();
function startAccordion() 
{
    for (var i=1; i<=Object.keys(description).length; i++){
        document.getElementById("description").innerHTML += 
            "<button class='accordion'>Trail " + description[i].Title + "</button>" + "<div class='accordion-panel'>" + "<p>" + description[i].Description + "<br></p>" + "</div>";
    }
}
</script>

Within the JavaScript the behavior when a panel on the accordion is clicked, is handled. 在JavaScript中,处理了单击手风琴上的面板时的行为。

var acc = document.getElementsByClassName('accordion');
var panel = document.getElementsByClassName('accordion-panel');

for (var i = 0; i < acc.length; i++) 
{
  acc[i].onclick = function()
  {         
    // Toggle between adding and removing the "active" class,
    //to highlight the button that controls the panel 
    this.classList.toggle("active");

    //Toggle between hiding and showing the active panel
    var panel = this.nextElementSibling;

    if (panel.style.display === "block") {
        panel.style.display = "none";
    } else {
        panel.style.display = "block";
    }
  }
}

Question: When the user clicks on a panel and this certain one is opened/activated, is there any possibility to get the index or any unique information about which of the panels has been clicked? 问题:当用户单击一个面板并打开/激活该面板时,是否有可能获得索引或有关哪个面板被单击的唯一信息?

I really need this information, because based on that, the related marker on a map should be shown. 我确实需要这些信息,因为基于此,应该显示地图上的相关标记。 I tried for hours and hours, but I could not find out, how to get the panel index. 我花了好几个小时尝试,但我找不到如何获取面板索引。 The variable "i" is unfortunately always 20, as 20 features are included. 不幸的是,变量“ i”始终为20,因为其中包含20个功能。 I think this is strange, because with acc[i] the related panel is opened/activated correctly. 我认为这很奇怪,因为使用acc [i]可以正确打开/激活相关的面板。

I would like to avoid using jQuery, but if you know that with my solution it is impossible I maybe could use jQuery, although I would have to create the whole accordion again. 我想避免使用jQuery,但是如果您知道使用我的解决方案是不可能的,那么我也许可以使用jQuery,尽管我不得不再次创建整个手风琴。

Following the link provided by Mohamed Abbas, this is the way it worked out. 遵循Mohamed Abbas提供的链接,这就是解决方法。 It is a scope issue, where within the for loop a new scope is created: 这是一个范围问题,其中在for循环中创建了一个新范围:

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

(function(index){
    acc[i].onclick = function(){

    //same as within the function given in the original post

          alert(index)  ;
    }    
})(i);

}

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

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