简体   繁体   中英

Is there a way for a JS script to affect only a part of my HTML code?

I'm using JS for the first time to display/hide images depending on if a button is pressed: if the "New version" button is pressed, I want the "new" image to appear and the "old" to hide, and vice-versa for the "Old version" button. Here's my Code:

 function showOld() { document.getElementById('new').style.display = "none"; document.getElementById('old').style.display = "block"; document.getElementById('btn-to-old').classList.toggle('btn-click'); document.getElementById('btn-to-new').classList.toggle('btn-click'); } function showNew() { document.getElementById('old').style.display = "none"; document.getElementById('new').style.display = "block"; document.getElementById('btn-to-new').classList.toggle('btn-click'); document.getElementById('btn-to-old').classList.toggle('btn-click'); }
 <div class="container"> <div class="row"> <h2>Index</h2> <div class="button-box"> <button type="button" onclick="showOld()" id="btn-to-old" class="btn-click">Old version</button> <button type="button" onclick="showNew()" id="btn-to-new">New version</button> </div> <img src="img/old-index.png" alt="old-index" id="old"> <img src="img/new-index.png" alt="new-index" id="new"> </div> </div> <div class="container"> <div class="row"> <h2>Home</h2> <div class="button-box"> <button type="button" onclick="showOld()" id="btn-to-old" class="btn-click">Old version</button> <button type="button" onclick="showNew()" id="btn-to-new">New version</button> </div> <img src="img/old-home.png" alt="old-index" id="old"> <img src="img/new-home.png" alt="new-index" id="new"> </div> </div>

Needless to say that this code doesn't work, and pressing the second set of buttons will change the first images and not the second ones. The idea is that only the two images directly below the buttons will be affected by the pressing of the button above them, and not the images down the page. I don't know how to get such a result.

I'm aware that I cannot use the same ID twice, but using classes somehow broke my page.

As you said, you can't have duplicate IDs, so you need to use classes.

Use DOM navigation operations to address the elements in the same container as the button you cllick.

 function showOld(el) { let container = el.closest(".container"); container.querySelector('.new').style.display = "none"; container.querySelector('.old').style.display = "block"; container.querySelector('.btn-to-old').classList.toggle('btn-click'); container.querySelector('.btn-to-new').classList.toggle('btn-click'); } function showNew(el) { let container = el.closest(".container"); container.querySelector('.old').style.display = "none"; container.querySelector('.new').style.display = "block"; container.querySelector('.btn-to-new').classList.toggle('btn-click'); container.querySelector('.btn-to-old').classList.toggle('btn-click'); }
 .btn-click { color: green; }
 <div class="container"> <div class="row"> <h2>Index</h2> <div class="button-box"> <button type="button" onclick="showOld(this)" class="btn-to-old btn-click">Old version</button> <button type="button" onclick="showNew(this)" class="btn-to-new">New version</button> </div> <img src="img/old-index.png" alt="old-index" class="old"> <img src="img/new-index.png" alt="new-index" class="new"> </div> </div> <div class="container"> <div class="row"> <h2>Home</h2> <div class="button-box"> <button type="button" onclick="showOld(this)" class="btn-to-old btn-click">Old version</button> <button type="button" onclick="showNew(this)" class="btn-to-new">New version</button> </div> <img src="img/old-home.png" alt="old-index" class="old"> <img src="img/new-home.png" alt="new-index" class="new"> </div> </div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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