Axel Richter: "div" onClick öffnen und schliessen

Beitrag lesen

Hallo,

Wie bekomme ich in die unten stehende Funktion ( function display_links(sender)) zusätzlich rein, dass sich der verschachtelte div (<div id="thema_1_cont") bei Mausklick auf "Configuration und Pfeil" nicht nur schließt, sondern auch bei erneutem Mausklick wieder öffnet?
function display_links(sender) {
   var cont = document.getElementById(sender.id+"_cont");

Wofür brauchst Du dieses Objekt?

var tag = document.getElementsByTagName('div');

tag ist ein Array aller div-Elemente im document

for (i=0;i<tag.length;i++) {
     if (tag[i].id.indexOf('_cont')!=-1){
       tag[i].style.display='none';

Alle div-Elemente, deren id den String "_cont" beinhaltet werden ausgeblendet.

}
   }
}

Willst Du bei Click alle divs ausblenden?  Oder suchst Du eher etwas wie das hier?
Einen genehmen Dokumenttyp musst Du noch einfügen.

<html>
<head>
<title></title>
<script type="text/javascript">
<!--
function kapitelEA(kap) {
  if (document.getElementById("Button" + kap).value=="-") {
    document.getElementById("Kapitel" + kap).style.display="none";
    document.getElementById("Button" + kap).value="+";
  } else {
    document.getElementById("Kapitel" + kap).style.display="block";
    document.getElementById("Button" + kap).value="-";
  }
}

//-->
</script>
</head>
<body>
<div>Kapitel1<input type="button" name="btn1" value="-" id="Button1" onclick="kapitelEA(1);" style="width:16px; height:16px; line-height:8px;"></div>
<div id="Kapitel1" style="border:1px solid black;">
 Inhalt von Kapitel 1 Inhalt von Kapitel 1 Inhalt von Kapitel 1 Inhalt von Kapitel 1 Inhalt von Kapitel 1 Inhalt von Kapitel 1 Inhalt von Kapitel 1
</div>
<div>Kapitel2<input type="button" name="btn2" value="-" id="Button2" onclick="kapitelEA(2);" style="width:16px; height:16px; line-height:8px;"></div>
<div id="Kapitel2" style="border:1px solid black;">
 Inhalt von Kapitel 2 Inhalt von Kapitel 2 Inhalt von Kapitel 2 Inhalt von Kapitel 2 Inhalt von Kapitel 2 Inhalt von Kapitel 2 Inhalt von Kapitel 2
</div>
<div>Kapitel3<input type="button" name="btn3" value="-" id="Button3" onclick="kapitelEA(3);" style="width:16px; height:16px; line-height:8px;"></div>
<div id="Kapitel3" style="border:1px solid black;">
 Inhalt von Kapitel 3 Inhalt von Kapitel 3 Inhalt von Kapitel 3 Inhalt von Kapitel 3 Inhalt von Kapitel 3 Inhalt von Kapitel 3 Inhalt von Kapitel 3
</div>
</body>
</html>

viele Grüße

Axel