循环功能document.getElementById()

时间:2019-04-06 09:15:17

标签: javascript html loops onclick id

所以我正在使用三个唯一的ID“ titleselected0”,“ titleselected1”,“ titleselected2”。每个函数都有一个onclick函数,该函数运行函数“ myFunction0”,“ myFunction1”,“ myFunction2”。我正在使用它来更改样式。我的问题是我是否可以使用循环,所以我只需要使用一个函数而不是三个函数?

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
        <style type="text/css">.collapse {
           display: none;
           }
           .collapse.in {
           display: block;
           }
           .title {
           background-color: #005FAA;
           border: none;
           border-radius: 0;
           padding: 30px;
           }
           .title:hover {
           background-color: #009cde;
           border: none;
           border-radius: 0;
           padding: 30px;
           }
           .container{
           margin-top:25px;
           float:left; 
           width: 32%; 
           margin-right: 2%;
           }
           .container:last-child{
           margin-right:0px;
           width:32%;}
           @media only screen and (max-width: 800px) {
           .container{
           margin-top:15px;
           width: 100%;
           }
           .container:last-child{
           margin-top:15px;
           width: 100%;
           }
           }
           .titleselected{
           background-color: #009cde;
           border:none; 
           border-radius:0; 
           padding: 30px;
           }
        </style>
        <div>
           <div class="container">
              <div class="title" data-toggle="collapse" href="#collapse" id="titleselected0" onclick="myFunction0()">
                 <h4><a href="#collapse" style="color:white; text-decoration:none; text-align:center;">System Information</a></h4>
              </div>
              <div class="collapse" id="collapse" style="background-color:#005FAA;transition: width 200ms ease-out, height 200ms ease-out;">
                 <div style="color:white; padding: 30px;border:none;">
                    <p><strong>TestingTestingTestingTestingTestingTestingTesting</strong></p>
                    <ul>
                       <li>TestingTestingTestingTestingTestingTestingTesting</li>
                       <li>TestingTestingTestingTestingTestingTestingTesting</li>
                       <li>TestingTestingTestingTestingTestingTestingTesting</li>
                    </ul>
                    <p><strong>TestingTestingTestingTestingTestingTestingTesting: </strong></p>
                    <ul>
                       <li>TestingTestingTestingTestingTestingTestingTesting</li>
                       <li>TestingTestingTestingTestingTestingTestingTesting</li>
                       <li>TestingTestingTestingTestingTestingTestingTesting</li>
                       <li>TestingTestingTestingTestingTestingTestingTesting</li>
                    </ul>
                 </div>
              </div>
           </div>
           <div class="container">
              <div class="title" data-toggle="collapse" href="#collapse2" id="titleselected1" onclick="myFunction1()">
                 <h4><a href="#collapse2" style="color:white;text-decoration:none;text-align:center;">Product Specification</a></h4>
              </div>
              <div class="collapse" id="collapse2" style="background-color:#005FAA;transition: width 200ms ease-out, height 200ms ease-out;">
                 <div style="color:white; padding: 30px;border:none;">
                    <p><strong>TestingTestingTestingTestingTestingTestingTesting:</strong></p>
                    <ul>
                       <li>TestingTestingTestingTestingTestingTestingTesting</li>
                       <li>TestingTestingTestingTestingTestingTestingTesting</li>
                       <li>TestingTestingTestingTestingTestingTestingTesting</li>
                       <li>TestingTestingTestingTestingTestingTestingTesting</li>
                       <li>TestingTestingTestingTestingTestingTestingTesting</li>
                       <li>TestingTestingTestingTestingTestingTestingTesting</li>
                    </ul>
                 </div>
              </div>
           </div>
           <div class="container">
              <div class="title" data-toggle="collapse" href="#collapse3" id="titleselected2" onclick="myFunction2()">
                 <h4><a href="#collapse3" style="color:white;text-decoration:none;text-align:center;">Case Study</a></h4>
              </div>
              <div class="collapse" id="collapse3" style="background-color:#005FAA;transition: width 200ms ease-out, height 200ms ease-out;">
                 <div style="color:white; padding: 30px;border:none;">
                    <p><strong>TestingTestingTestingTestingTestingTestingTesting: </strong></p>
                    <ul>
                       <li>TestingTestingTestingTestingTestingTestingTesting</li>
                       <li>TestingTestingTestingTestingTestingTestingTesting <a href="http://portfolio.cpl.co.uk/CIBSE/201904/40/">appeared in the CIBSE Journal</a></li>
                    </ul>
                 </div>
              </div>
           </div>
        </div>
        <script>
           function myFunction0() {
              var element = document.getElementById("titleselected0");
              element.classList.toggle("titleselected");
           }
           
           function myFunction1() {
              var element = document.getElementById("titleselected1");
              element.classList.toggle("titleselected");
           }
           
           function myFunction2() {
              var element = document.getElementById("titleselected2");
              element.classList.toggle("titleselected");
           }
           
           
        </script>

1 个答案:

答案 0 :(得分:3)

这些元素已经具有class中的title,因此您可以遍历所有.title元素并附加侦听器:

document.querySelectorAll('.title').forEach((element) => {
  element.addEventListener('click', () => {
    element.classList.toggle("titleselected");
  });
});

(随意删除其所有id和内联属性onclick处理程序-最好使用Javascript(而不是HTML)附加侦听器

您还可以在具有所有.title的容器上使用事件委托:

<div class="container-for-all">
   <div class="container">
     ...
   </div>
   <div class="container">
     ...
   </div>
   <!-- etc -->

document.querySelector('.container-for-all').addEventListener('click', ({ target }) => {
  const closestTitle = target.closest('.title');
  if (!closestTitle) {
    return;
  }
  closestTitle.classList.toggle('titleselected');
});