2010-01-28 85 views
1

我想關閉一個div時點擊另一個div。目前,它一次打開多個div。jquery slidetoggle打開1格,並關閉另一個

JQUERY:

$(document).ready(function() { 

    $(".dropdown dt a").click(function() { 
     var dropID = $(this).closest("dl").attr("id"); 
     $("#"+dropID+" dd ul").slideToggle(200); 
     return false; 
    }); 

    $(".dropdown dd ul li a").click(function() { 
     var dropID = $(this).closest("dl").attr("id"); 
     var text = $(this).html(); 
     var selVal = $(this).find(".dropdown_value").html(); 
     $("#"+dropID+" dt a").html(text); 
     $("#"+dropID+" dd ul").hide(); 
     return false; 
    }); 

    $("dl[class!=dropdown]").click(function() { 
     $(".dropdown dd ul").hide(); 
     return false; 
    }); 


    $("id!=quotetoolContainer").click(function() { 
     $(".dropdown dd ul").hide(); 
     return false; 
    }); 

    $('body').click(function() { 
     $(".dropdown dd ul").hide(); 
     return false; 
    }); 

    $('.productSelection').children().hover(function() { 
     $(this).siblings().stop().fadeTo(200,0.5); 
    }, function() { 
     $(this).siblings().stop().fadeTo(200,1); 
    }); 

}); 

HTML:

<div id="quotetoolContainer"> 
    <div class="top"></div> 
    <div id="quotetool"> 
    <h2>Instant Price Calculator</h2> 
    <p>Document Type</p> 
    <dl id="docType" class="dropdown"> 
     <dt><a href="#"><span>Select a Document Type</span></a></dt> 
     <dd> 
     <ul> 
      <li><a href="#" id="1">Datasheets<span class="value">Datasheets</span></a></li> 
      <li><a href="#">Manuals<span class="value">Manuals</span></a></li> 
      <li><a href="#">Brochures<span class="value">Brochures</span></a></li> 
      <li><a href="#">Newsletters<span class="value">Newsletters</span></a></li> 
      <li><a href="#">Booklets<span class="value">Booklets</span></a></li> 
     </ul> 
     </dd> 
    </dl> 
    <p>Flat Size</p> 
    <dl id="flatSize" class="dropdown"> 
     <dt><a href="#">8.5" x 11"<span class="value">8.5" x 11"</span></a></dt> 
     <dd> 
     <ul> 
      <li><a href="#">8.5" x 11"<span class="value">8.5" x 11"</span></a></li> 
      <li><a href="#">11" x 17"<span class="value">11" x 17"</span></a></li> 
     </ul> 
     </dd> 
    </dl> 
    <p>Full Color or Black &amp; White?</p> 
    <dl id="color" class="dropdown"> 
     <dt><a href="#">Full Color<span class="value">Full Color</span></a></dt> 
     <dd> 
     <ul> 
      <li><a href="#">Full Color<span class="value">Full Color</span></a></li> 
      <li><a href="#">Black &amp; White<span class="value">Black &amp; White</span></a></li> 
     </ul> 
     </dd> 
    </dl> 
    <p>Paper</p> 
    <dl id="paper" class="dropdown"> 
     <dt><a href="#">Value White Paper (20 lb.)<span class="value">Value White Paper (20 lb.)</span></a></dt> 
     <dd> 
     <ul> 
      <li><a href="#">Value White Paper (20 lb.)<span class="value">Value White Paper (20 lb.)</span></a></li> 
      <li><a href="#">Premium White Paper (28 lb.)<span class="value">Premium White Paper (28 lb.)</span></a></li> 
      <li><a href="#">Glossy White Text (80 lb.) - Recycled<span class="value">Glossy White Text (80 lb.) - Recycled</span></a></li> 
      <li><a href="#">Glossy White Cover (80 lb.) - Recycled<span class="value">Glossy White Cover (80 lb.) - Recycled</span></a></li> 
     </ul> 
     </dd> 
    </dl> 
    <p>Folding</p> 
    <dl id="folding" class="dropdown"> 
     <dt><a href="#">Fold in Half<span class="value">Fold in Half</span></a></dt> 
     <dd> 
     <ul> 
      <li><a href="#">Fold in Half<span class="value">Fold in Half</span></a></li> 
      <li><a href="#">Tri-Fold<span class="value">Tri-Fold</span></a></li> 
      <li><a href="#">Z-Fold<span class="value">Z-Fold</span></a></li> 
      <li><a href="#">Double-Parallel Fold<span class="value">Double-Parallel Fold</span></a></li> 
     </ul> 
     </dd> 
    </dl> 
    <p>Three-Hole Drill</p> 
    <dl id="drill" class="dropdown"> 
     <dt><a href="#">No<span class="value">No</span></a></dt> 
     <dd> 
     <ul> 
      <li><a href="#">No<span class="value">No</span></a></li> 
      <li><a href="#">Yes<span class="value">Yes</span></a></li> 
     </ul> 
     </dd> 
    </dl> 
    <p>Qty</p> 
    <dl id="quantity" class="dropdown"> 
     <dt><a href="#">50<span class="value">50</span></a></dt> 
     <dd> 
     <ul> 
      <li><a href="#">50<span class="value">50</span></a></li> 
      <li><a href="#">100<span class="value">100</span></a></li> 
      <li><a href="#">150<span class="value">150</span></a></li> 
      <li><a href="#">200<span class="value">200</span></a></li> 
      <li><a href="#">250<span class="value">250</span></a></li> 
      <li><a href="#">500<span class="value">500</span></a></li> 
      <li><a href="#">750<span class="value">750</span></a></li> 
      <li><a href="#">1,000<span class="value">1,000</span></a></li> 
      <li><a href="#">1,500<span class="value">1,500</span></a></li> 
      <li><a href="#">2,000<span class="value">2,000</span></a></li> 
      <li><a href="#">2,500<span class="value">2,500</span></a></li> 
      <li><a href="#">3,000<span class="value">3,000</span></a></li> 
      <li><a href="#">3,500<span class="value">3,500</span></a></li> 
      <li><a href="#">4,000<span class="value">4,000</span></a></li> 
      <li><a href="#">4,500<span class="value">4,500</span></a></li> 
      <li><a href="#">5,000<span class="value">5,000</span></a></li> 
      <li><a href="#">5,500<span class="value">5,500</span></a></li> 
      <li><a href="#">6,000<span class="value">6,000</span></a></li> 
      <li><a href="#">6,500<span class="value">6,500</span></a></li> 
      <li><a href="#">7,000<span class="value">7,000</span></a></li> 
      <li><a href="#">7,500<span class="value">7,500</span></a></li> 
      <li><a href="#">8,000<span class="value">8,000</span></a></li> 
      <li><a href="#">8,500<span class="value">8,500</span></a></li> 
      <li><a href="#">9,000<span class="value">9,000</span></a></li> 
      <li><a href="#">9,500<span class="value">9,500</span></a></li> 
      <li><a href="#">10,000<span class="value">10,000</span></a></li> 
      <li><a href="#">12,500<span class="value">12,500</span></a></li> 
      <li><a href="#">15,000<span class="value">15,000</span></a></li> 
      <li><a href="#">17,500<span class="value">17,500</span></a></li> 
      <li><a href="#">20,000<span class="value">20,000</span></a></li> 
     </ul> 
     </dd> 
    </dl> 
    <div id="priceTotal"> 
     <div class="priceText">Your Price:</div> 
     <div class="price">$29.00</div> 
     <div class="clear"></div> 
    </div> 
    <div id="buttonQuoteStart"><a href="#" title="Start Printing">Start Printing</a></div> 
    </div> 
    <div class="bottom"></div> 
</div> 
+1

後HTML太 可能是一個更簡單的方法來做到這一點 – davidosomething 2010-01-28 19:52:29

+0

好主意!發佈。 – 2010-01-28 20:45:52

回答

2

找到我的解決方案!

$(".dropdown dt a").click(function() { 
    var dropID = $(this).closest("dl").attr("id"); 

    if ($("#"+dropID+" dd ul.opened").length) { 
    $('ul.opened').slideToggle(200).removeClass('opened'); 
    } else { 
    $('ul.opened').slideToggle(200).removeClass('opened'); 
    $("#"+dropID+" dd ul").slideToggle(200).addClass('opened'); 
    } 
    return false;  
}); 
0

上元素的集合使用的slideToggle是要打開所有封閉的人,並關閉所有打開的。使用一個類來跟蹤哪一個被打開,然後只滑動你想要打開和關閉的特定的ul可能是一個更好的解決方案。就像這樣:

$(".dropdown dt a").click(function() { 
    $('ul.opened').slideToggle(200).removeClass('opened'); 
    $(this).closest("dl").find("dd ul").slideToggle(200).addClass('opened'); 
    return false; 
}); 

就我個人而言,我喜歡使用slideUp和slideDown而不是slideToggle。這樣我就知道具體發生了什麼。

+0

它實際上並沒有在點擊時打開所有的DIV。它打開適當的DIV,但是當你點擊一個新的DIV時,它會打開但不關閉以前的DIV。這有助於解釋一下嗎?您的建議向我展示了一種新的可能方向,謝謝! – 2010-01-28 20:07:29

+0

然後,它不會切換打開的原因是因爲您只訪問DOM中的某個特定的ul(最接近您點擊的錨的那個)。您需要一種切換打開的和您打算啓動的打開的方法。很高興我能幫上忙。 – munch 2010-01-28 20:20:42

+0

再次感謝蒙克,想通了解併發布瞭解決方案。 – 2010-01-28 21:31:10

相關問題