根据页面加载日期删除<li>

时间:2018-06-26 12:38:34

标签: javascript jquery html css

我正在寻找可以动态删除手风琴中传递的事件的代码。这是手风琴:

 <div class="container3">

          <div class="accordion">
            <dl>
              <dt>
                <a href="#accordion1" aria-expanded="false" aria-controls="accordion1" class="accordion-title accordionTitle js-accordionTrigger">Travel Territory</a>
              </dt>
              <dd class="accordion-content accordionItem is-collapsed" id="accordion1" aria-hidden="true">
                <p><li>Test 1</li>
                <li>Test 2</li></p>
              </dd>
              <dt>
                <a href="#accordion2" aria-expanded="false" aria-controls="accordion2" class="accordion-title accordionTitle js-accordionTrigger">Catch me at these schools!</a>
              </dt>
              <dd class="accordion-content accordionItem is-collapsed" id="accordion2" aria-hidden="true">

                <p><ul id="myList">
                <li>03/24/2018 at Upper Darby High School 1</li>
                <li>03/24/2019 at Upper Darby High School 2</li>
                </ul>
                </p>
              </dd>
            </dl>
          </div>
        </div>

在带有Mylist ID的UL中,您将看到日期。出于测试目的,我做了两年不同的尝试,但通常情况下,彼此相隔几天。我正在寻找找到某种代码,以在事件通过后删除这些

  • 。我希望在该列表中放置15个事件,并且不想在每次事件后都将其删除。有没有人有任何代码可以根据日期和页面加载来删除项目?

    谢谢, 罗布

  • 1 个答案:

    答案 0 :(得分:0)

    您的问题的小片段。

    首先我们得到了李的全部。

    然后我们通过拆分获得日期。在这里很重要。如果日期更改了位置或格式,它将刹车。我建议通过data属性添加dia。

    然后我们将li的日期与今天进行比较。

    如果满足您的条件,我们将其删除。

    $('#myList').children().each( function(){
      let liDate = new Date(this.innerHTML.split(' ')[0]);
      let today = new Date;
      if(today > liDate){
        $(this).remove();
      }
    })
    

    $('#myList').children().each( function(){
      let liDate = new Date(this.innerHTML.split(' ')[0]);
      let today = new Date;
      if(today > liDate){
        $(this).remove();
      }
    })
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div class="container3">
    
              <div class="accordion">
                <dl>
                  <dt>
                    <a href="#accordion1" aria-expanded="false" aria-controls="accordion1" class="accordion-title accordionTitle js-accordionTrigger">Travel Territory</a>
                  </dt>
                  <dd class="accordion-content accordionItem is-collapsed" id="accordion1" aria-hidden="true">
                    <p><li>Test 1</li>
                    <li>Test 2</li></p>
                  </dd>
                  <dt>
                    <a href="#accordion2" aria-expanded="false" aria-controls="accordion2" class="accordion-title accordionTitle js-accordionTrigger">Catch me at these schools!</a>
                  </dt>
                  <dd class="accordion-content accordionItem is-collapsed" id="accordion2" aria-hidden="true">
    
                    <p><ul id="myList">
                    <li>03/24/2018 at Upper Darby High School 1</li>
                    <li>03/24/2019 at Upper Darby High School 2</li>
                    </ul>
                    </p>
                  </dd>
                </dl>
              </div>
            </div>