简体   繁体   English

如何选择具有相同类名 JavaScript 的多个元素

[英]How to Select multiple elements with the same class name JavaScript

what I am trying to do just show some text on click with JavaScript by giving it an active class.as you can see there are two buttons with same class of parent as-well as the children only the contents are different.我想要做的只是通过给它一个活动类来显示一些文本点击JavaScript。你可以看到有两个按钮具有相同的父类以及只有子类的内容不同。 but only the first button is working because I am passing the array value of [0].is there a way to do all the buttons?但只有第一个按钮在工作,因为我正在传递 [0] 的数组值。有没有办法完成所有按钮? thank you..谢谢你..

 let arrowbtn = document.getElementsByClassName("story-contents-title")[0]; let info = document.getElementsByClassName("story-contents-discription")[0]; arrowbtn.addEventListener("click", () => { info.classList.toggle("active"); arrowbtn.classList.toggle("active"); });
 .story-title { color: #377dff; } .story-contents { margin-top: 1rem; } .story-contents-title { background-color: white; color: white; border: solid 2px #377dff; padding: 0.5rem; font-size: 4rem; border-radius: 10px; width: 20rem; color: #377dff; } .story-contents-title svg { stroke: #377dff; transition: all 0.5s ease-out; } .story-contents-title.active svg { transform: rotate(90deg); } .story-contents-discription { margin-top: 0.5rem; padding: 1rem; color: white; background-color: #377dff; border-radius: 10px; display: none; } .story-contents-discription.active { display: block; }
 <div class="story"> <h1 class="story-title">Our Story</h1> <div class="story-contents"> <button class="story-contents-title">2021<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-arrow-big-right" width="34" height="34" viewBox="0 0 24 24" stroke-width="1.5" stroke="" fill="none" stroke-linecap="round" stroke-linejoin="round"> <path stroke="none" d="M0 0h24v24H0z" fill="none"/> <path d="M4 9h8v-3.586a1 1 0 0 1 1.707 -.707l6.586 6.586a1 1 0 0 1 0 1.414l-6.586 6.586a1 1 0 0 1 -1.707 -.707v-3.586h-8a1 1 0 0 1 -1 -1v-4a1 1 0 0 1 1 -1z" /> </svg></button> <p class="story-contents-discription">Wins 'Outstanding Crisis Finance Innovation 2021 (Asia Pacific) Award' by Global Finance Magazine <br> Launches Step Up Credit Card <br> Wins 'Digital Lending Award' at the Fintech India Innovation Awards <br> Wins “Excellence in Consumer Lending” at India Digital Awards</p> </div> <div class="story-contents"> <button class="story-contents-title">2020<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-arrow-big-right" width="34" height="34" viewBox="0 0 24 24" stroke-width="1.5" stroke="" fill="none" stroke-linecap="round" stroke-linejoin="round"> <path stroke="none" d="M0 0h24v24H0z" fill="none"/> <path d="M4 9h8v-3.586a1 1 0 0 1 1.707 -.707l6.586 6.586a1 1 0 0 1 0 1.414l-6.586 6.586a1 1 0 0 1 -1.707 -.707v-3.586h-8a1 1 0 0 1 -1 -1v-4a1 1 0 0 1 1 -1z" /> </svg></button> <p class="story-contents-discription"> Upgrades in-house systems to enable work-from-home for employees <br> Launches Free Credit Report in Regional Languages </p> </div> </div>

I would delegate and navigate within the container我会在容器内委托和导航

 document.querySelector(".story").addEventListener("click", function(e) { // any click in the story div const tgt = e.target.closest("button"); // we click inside a button somewhere, closest makes sure it is the button itself we are getting tgt.closest(".story-contents") // the div holding button AND paragraph .querySelector(".story-contents-discription") // the paragraph .classList.toggle("active"); // toggle active on paragraph tgt.classList.toggle("active"); // toggle active on button });
 .story-title { color: #377dff; } .story-contents { margin-top: 1rem; } .story-contents-title { background-color: white; color: white; border: solid 2px #377dff; padding: 0.5rem; font-size: 4rem; border-radius: 10px; width: 20rem; color: #377dff; } .story-contents-title svg { stroke: #377dff; transition: all 0.5s ease-out; } .story-contents-title.active svg { transform: rotate(90deg); } .story-contents-discription { margin-top: 0.5rem; padding: 1rem; color: white; background-color: #377dff; border-radius: 10px; display: none; } .story-contents-discription.active { display: block; }
 <div class="story"> <h1 class="story-title">Our Story</h1> <div class="story-contents"> <button class="story-contents-title">2021<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-arrow-big-right" width="34" height="34" viewBox="0 0 24 24" stroke-width="1.5" stroke="" fill="none" stroke-linecap="round" stroke-linejoin="round"> <path stroke="none" d="M0 0h24v24H0z" fill="none"/> <path d="M4 9h8v-3.586a1 1 0 0 1 1.707 -.707l6.586 6.586a1 1 0 0 1 0 1.414l-6.586 6.586a1 1 0 0 1 -1.707 -.707v-3.586h-8a1 1 0 0 1 -1 -1v-4a1 1 0 0 1 1 -1z" /> </svg></button> <p class="story-contents-discription">Wins 'Outstanding Crisis Finance Innovation 2021 (Asia Pacific) Award' by Global Finance Magazine <br> Launches Step Up Credit Card <br> Wins 'Digital Lending Award' at the Fintech India Innovation Awards <br> Wins “Excellence in Consumer Lending” at India Digital Awards</p> </div> <div class="story-contents"> <button class="story-contents-title">2020<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-arrow-big-right" width="34" height="34" viewBox="0 0 24 24" stroke-width="1.5" stroke="" fill="none" stroke-linecap="round" stroke-linejoin="round"> <path stroke="none" d="M0 0h24v24H0z" fill="none"/> <path d="M4 9h8v-3.586a1 1 0 0 1 1.707 -.707l6.586 6.586a1 1 0 0 1 0 1.414l-6.586 6.586a1 1 0 0 1 -1.707 -.707v-3.586h-8a1 1 0 0 1 -1 -1v-4a1 1 0 0 1 1 -1z" /> </svg></button> <p class="story-contents-discription"> Upgrades in-house systems to enable work-from-home for employees <br> Launches Free Credit Report in Regional Languages </p> </div> </div>

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

相关问题 如何通过与多个类名匹配的类名选择javascript中的元素? - How to select elements in javascript by class name matching with multiple class names ? Select 具有相同 class 名称的多个跨度元素 - Select multiple span elements with same class name 使用javascript选择同一类的多个(但不是全部)元素 - Select Multiple (but not all) Elements of the Same Class with javascript 如何通过 JavaScript 的 class 名称来 select 偶数元素? - How to select even elements by class name with JavaScript? jQuery-将函数应用于具有相同类名的多个选择元素 - jQuery - apply function to multiple select elements with same class name 使用JavaScript一次更改具有相同类名的多个元素的样式? - Changing multiple elements' styles with a same class name at once with JavaScript? 如何使用Javascript更改具有相同类名的多个元素? - How do I change multiple elements with same class name using Javascript? 如何在单击时在具有相同类名的多个元素上切换类 - How to toggle class on multiple elements with same class name on click jQuery-如何在文档区域中选择2个具有相同类名的元素? - Jquery - How to select 2 elements with same class name in a document area? 如何只选择具有相同类名的元素列表中的第一个元素 - How select only the first element of a list of elements with the same class name
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM