如何删除JS中多余的元素内容?

时间:2019-07-25 15:27:05

标签: javascript jquery html dom attributes

我正在研究下面要检查的html代码。在下面的class="schedule-wrapper"代码中,有一个属性 data-timezone,其值为"et"。点击按钮后data-timezone属性值会发生变化,如以下下面的屏幕截图所示。单击按钮(PTMTCTETATNT),页面刷新不会发生。

enter image description here

<div class="schedule-wrapper" id="js-schedule" data-timezone="et">         /* The value of data-timezone attribute changes on button click from the Screenshot below */ 

   <!-- List of button start -->
   <div class="schedule-action-bar">
      <div class="schedule-timezone-filter">
         Select your timezone:            
         <ul id="js-timezone-picker">
            <li>
               <button id="js-time-ct" class="" data-timezone="ct">CT</button>
            </li>
            <li>
               <button id="js-time-et" class="" data-timezone="et">ET</button>
            </li>
         </ul>
      </div>
    </div>
    <!-- List of button end -->

    <!-- .schedule-show -->
    <div class="schedule-show">
        <div class="schedule-show-time">
              <time datetime="02:45 24-07-2019" data-timezone="ct">July 24  02:45</time>
               <time datetime="03:45 24-07-2019" data-timezone="et">July 24  03:45</time>           /* When timezone is eastern standard time */
        </div>
    </div>
    <!-- .schedule-show -->
    <div class="schedule-show">
        <div class="schedule-show-time">
              <time datetime="04:00 24-07-2019" data-timezone="ct">July 24  04:00</time>
              <time datetime="05:00 24-07-2019" data-timezone="et">July 24  05:00</time>            /* When timezone is eastern standard time */
        </div>
    </div>
   <!-- .schedule-show -->

</div>

问题陈述:

我想知道我需要添加什么Javascript代码,以便当值与上一行含义相同时,我们可以隐藏(或重影/使其在视觉上不太突出)日期 在日期与上一行相同的行上不显示日期,以便按日期显示可视时间。

简而言之,如上面的屏幕截图所述,我不希望该日期用圆圈标记。

1 个答案:

答案 0 :(得分:1)

I made a fiddle here

// we have to iterate backwards... actually maybe we don't..
$($('.schedule-show').get().reverse()).each(function(i, div){
  let time = $(div).find('[data-timezone="et"]')
  let nextTime = $(div).find('+ .schedule-show [data-timezone="et"]')
  if(nextTime[0]){
    // check if the dates match
    if(nextTime.text().match(/\w+ \d+/)[0] === time.text().match(/\w+ \d+/)[0]){
      // "white out" the dates that match
      nextTime.html(nextTime.text().replace(/(\w+ \d+)/, "<font color=\"white\">$1</font>"))
    }
  }
})