隐藏“dd”后如何隐藏“dt”

时间:2010-01-15 06:25:07

标签: javascript jquery

我有一个触发的javascript函数来隐藏具有相同类名的“dd”。但问题是,有时候,只有dt是孤立的。例如,第一个dt dd块,当我隐藏所有3 dd时。 dt(今天)仍然存在。当没有dd关联时我也想隐藏dt。我怎样才能做到这一点?

<dl>
    <dt>Today</dt>
    <dd class="Admissions3">14:59 > Admission: Transfer, B3FOL, Metroplex Hospital</dd>
    <dd class="Admissions3">12:00 > IM: Review by Me, Wound healing well. Could need further follow-up...</dd>
    <dd class="Admissions3" style="color: Black">09:42 > LAB: WOUND CULTURE, Staph aureus (Pending)</dd> <dt>Yesterday</dt>

    <dd class="Laboratory3">16:40 > LAB: UMCS, Blood, Enterococcus sp. Light growth of (Pending)</dd>
    <dd class="Surgery3">10:35 > SUR: Right Total Hip Replacement</dd> <dt>Two days ago</dt>

    <dd class="Admissions3">16:25 > ADT: Transfer, ICU, Metroplex Hospital</dd>
    <dd class="Laboratory3">13:15 > LAB: UMCS, Swab, No growth</dd>
    <dd class="Admissions3">13:10 > ADT: Admit EXC HUTCHINSONS MELANOTIC FRECKLE</dd> <dt>Four days ago</dt>

    <dd class="CentralLine3">14:24 > RM: Parking Incident</dd>
    <dd class="CentralLine3">14:05 > RX: Doxycycline 4g Daily</dd>
    <dd class="Admissions3">13:20 > ADT: Podiatry</dd> <dt>9/7/09</dt>

    <dd class="Laboratory3">15:32 > LAB: UMCS, Blood, No growth</dd>
    <dd class="Pharmacy3">10:18 > IM: Isolation Precautions - High</dd> <dt>3/5/09</dt>

    <dd class="Surgery3">11:45 > SUR: Left Total Hip Replacement</dd>
    <dd class="Admissions3">09:20 > ADM: Transfer, B3ROD, Real World Hospital</dd>
    <dd class="Admissions3">09:17 > ADM: Total Hip Replacement, Real World Hospital</dd>
</dl>

2 个答案:

答案 0 :(得分:2)

我觉得应该有更好的方法,但是这个怎么样?

$('.Admissions3').hide();

$('dt').each(function() {
  var $dt = $(this);

  if (!$dt.next(':visible').is('dd'))
    $dt.hide();
});

您可以在此处看到它:http://jsbin.com/orolo3/edit

答案 1 :(得分:0)

这段代码可能会有所帮助。

var $dl = $('dl'); // hook this up to your DL

$dl.find('dt').each(function() {
  var $dt = $(this), $next = $dt.nextAll(':visible').slice(0,1);

  // if the next visible item is also a dt, or there isn't a next visible item
  if ($next.is('dt') || $next.length == 0)
  {
    $dt.hide();
  } else {
    $dt.show();
  }
});