简体   繁体   English

如果有更多具有相同类的div,则将JavaScript函数应用于1 div

[英]Apply a javaScript function to 1 div when there are more divs with the same class

I'm trying to make a simple timeline that informs users. 我正在尝试为用户提供一个简单的时间表。 When they click on a date, there should be some sort of "accordion" system that drops down and give more information. 当他们单击日期时,应该有某种“手风琴”系统可以显示下来并提供更多信息。 When they click on it again, the "accordion" closes again. 当他们再次单击它时,“手风琴”再次关闭。

I've included some pictures to make it somewhat more clear: 我提供了一些图片,使其更加清晰:

And: 和:

The first image shows what the user is seeing when he gets onto the page, the second picture shows what he sees when he clicks on 1 of the elements. 第一张图片显示用户进入页面时所看到的内容,第二张图片显示用户单击其中一个元素时所看到的内容。

The problem ive at the moment is when he clicks on 1 day, all the information is shown . 目前的问题是, 当他单击1天时,将显示所有信息 I dont know how i can get some sort of index so only that particular day shows its hidden information. 我不知道如何获取某种索引,因此仅在特定的日子显示其隐藏的信息。

At the moment I've the following code: 目前,我有以下代码:

JavaScript JavaScript的

        $counter=1;
        $(document).ready(function(){
          $(".tijdlineElement").click(function(){

            $(".tijdlineElementHidden").slideToggle("slow");

            if($counter == 1){
            getElementsByClassName("tijdlineElementHidden").style.display = "block";
            $counter = 2
            }
            else{
         getElementByClass("tijdlineElementHidden").style.display = "none";

            $counter =1
            }
          });
        });

and the PHP to make 1 Day: 和PHP赚1天:

    echo "<div class='tijdlineElement'>";
echo "<div class='tijdlineP2Element' >" . $topic['Uur']."<br /><br />" . $topic['Beschrijving'] . "</div>";

echo "<div class='tijdlinePElement'>". $newDate . '<br />'. $newDate1 . ' '. $newDate1a . '<br />' . $newDate2 ."</div>";
echo "<img src='images/meerFase1.png'/>";


echo "</div>";
echo "<div class='tijdlineElementHidden' style='display:none;'>";
echo "<div class='tijdlineP2Element'>" . $topic['LangeBeschrijving'] . "</div>";
echo "<div class='tijdlinePElement'></div>";
echo "</div><br />";

The issue is, when a user clicks on 1 date, all the information from the other days get revealed aswell. 问题是,当用户单击一个日期时,其他日期的所有信息也会被显示出来。

So my question is: How can i get access to that particular div, so only the information from the selected(the div that was clicked on) div is shown? 所以我的问题是: 我怎样才能访问该特定的div,所以只显示所选div中的信息?

With your current code by using $(".tijdlineElement").click(function(){ } You are triggering the click event on all elements with that class. What you could do is use something like .each() and $(this) to scope it to your currently clicked element. 通过使用$(".tijdlineElement").click(function(){ }使用当前代码,您将触发该类所有元素的click事件。您可以使用.each()$(this)将其范围限制为当前单击的元素。

$(".tijdlineElement").each(function(){
   $(this).on({
       click:function()
       {
           $(this).slideToggle("slow");
           // other click function stuff
       }
   });
});

Quick Demo: 快速演示:

http://jsfiddle.net/9v2D5/ http://jsfiddle.net/9v2D5/

Updated Demo: 更新的演示:

http://jsfiddle.net/9v2D5/25/ http://jsfiddle.net/9v2D5/25/

Try this: 尝试这个:

var parent = $( this );
$( ".tijdlineElementHidden", parent ).slideToggle("slow");

It should toggle only the "tijdlineElementHidden" divs that are children to the clicked element. 它应仅将属于子级的“ tijdlineElementHidden” div切换到所单击的元素。

Loop through all elements that share the same class name, then addEventListener or attachEvent if IE8. 遍历共享相同类名的所有元素,然后添加IE8的addEventListenerattachEvent (Native JS solution) (本机JS解决方案)

var elements = document.querySelectorAll(".tijdlineElement");
var i = 0, length = elements.length;
for (i; i < length; i++) {
    if (document.addEventListener) {
        elements[i].addEventListener("click", function() {
            var element = this;
        });
    } else { // IE8 support
        elements[i].attachEvent("onclick", function() {
            var element = elements[i];
        });
    };
};

Instead of referencing the class, use $(this) to reference it: 而不是引用该类,而是使用$(this)来引用它:

 $(".tijdlineElement").click(function(){

   $(this).find(".tijdlineElementHidden").slideToggle("slow");
   .....the rest of the code...
 }

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

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