For example, we have some kind of div
, when clicking on a link that changes the class in another diva:
function active(element) { //ΠΡΠΈΡΠ°Π΅ΠΌ Π²ΡΠ΅ ΡΠΆΠ΅ Π½Π°ΠΆΠ°ΡΡΠ΅ ΡΠ»Π΅ΠΌΠ΅Π½ΡΡ for (var i = 1; i < N; i++) { var clear = document.getElementById("tab_" + i); clear.className = "cont" + i; } //ΠΡΠΈΡΠ²Π°ΠΈΠ²Π°Π΅ΠΌ Π½ΠΎΠ²ΡΠΉ ΠΊΠ»Π°ΡΡ Π½Π°ΠΆΠ°ΡΠΎΠΌΡ ΡΠ»Π΅ΠΌΠ΅Π½ΡΡ var block = document.getElementById("tab_" + element); if (block.className == "cont" + element) { block.className = "cont" + element + " is_active"; } }
<div><a href="#" onclick="active('1');">Π‘ΠΌΠ΅Π½ΠΈ ΠΌΠ΅Π½Ρ</a></div> <div><a href="#" onclick="active('2');">Π‘ΠΌΠ΅Π½ΠΈ ΠΌΠ΅Π½Ρ</a></div> <div id="tab_1" class="cont1 is_active"> <p>ΠΠ΅ΡΠ²ΡΠΉ Π΄ΠΈΠ²</p> </div> <div id="tab_2" class="cont2"> <p>ΠΡΠΎΡΠΎΠΉ Π΄ΠΈΠ²</p> </div>
From the code you can see that the active
function is called when you click on a specific link through onclick
. But how to make a call to this function so that only id
in the attributes? How should JS change, except that it will become some kind of "passive" handler? I can be wrong in the wording.