2017-02-17 236 views
1

我有這個工作完全像我想要的(見小提琴下面),除了我發現toggle()已被棄用。我想知道是否可以使用toggleClass()或hide()和show()來切換dropdownpanel的可見性。jQuery合併切換(),顯示()和隱藏()

$("#dropdown").click(function() { 
 
    $("#dropdownpanel").toggle(); 
 
}); 
 

 
$("#latinlink").click(function() { 
 
    $("#sometext").hide(); 
 
    $("#latin").show(); 
 
}); 
 

 
$("#back").click(function() { 
 
    $("#latin").hide(); 
 
    $("#sometext").show(); 
 
});
#dropdownpanel { 
 
    width: 236px; 
 
    height: 100vh; 
 
    background: rgba(00, 00, 00, 0.55); 
 
    color: #ffffff; 
 
    position: fixed; 
 
    top: 38px; 
 
    left: 0px; 
 
    display: none; 
 
    padding: 5px; 
 
} 
 

 
#latin { 
 
    width: 236px; 
 
    height: 100vh; 
 
    background: rgba(00, 00, 00, 0.55); 
 
    color: #ffffff; 
 
    position: fixed; 
 
    top: 38px; 
 
    left: 0px; 
 
    display: none; 
 
    padding: 5px; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
 
    <button id="dropdown"> 
 
     Dropdown 
 
    </button> 
 
    
 
    <div id="dropdownpanel"> 
 
     <p id="sometext">Click 
 
     <a href="#" id="latinlink">here</a> to read some latin... 
 
     </p> 
 
    </div> 
 
    
 
    <div id="latin"> 
 
    <button id="back"> 
 
    Back 
 
    </button> 
 
     <br /> Lorem ipsum dolor sit amet, epicuri argumentum constituam eum ea. Munere aliquip te nec, at nec dicat fuisset offendit, quo an sonet honestatis. Tempor facete sadipscing te his. Nam vivendo postulant reprehendunt te, offendit principes pri ex, at causae 
 
     oblique torquatos pro. 
 
     <br /> 
 
     <br /> Libris philosophia has eu, nam iriure alienum intellegam at. Nec appareat electram ne, amet nonumes per te. Usu eu nisl verterem, labore regione legimus in nam, vel saperet laboramus voluptatibus ut. Ad eam altera nemore. No nobis corpora pro, qui 
 
     ex tale suas dissentias, et iisque omittam inciderint sit. Usu ut case menandri vituperata. 
 
    </div>

JSFiddle

+0

我不明白你在問什麼。什麼_「它現在表現得像後退按鈕」_是什麼意思? – j08691

回答

0

當前的行爲是因爲當你點擊#latinlink元素是隱藏#dropdownpanel,這樣的話,當你再次點擊#dropdown按鈕,就會切換#dropdownpanel從隱藏到可見再次,這與#back按鈕的功能相同。

來解決這個問題,而完全不改變HTML可能是更新#dropdown點擊處理程序來測試是否#latin最簡單的方法是當前可見:

$("#dropdown").click(function() { 
 
    if ($("#latin").is(":visible")) 
 
    $("#dropdownpanel").hide(); 
 
    else 
 
    $("#dropdownpanel").toggle(); 
 
    $("#latin").hide(); 
 
}); 
 
    
 
$("#latinlink").click(function() { 
 
    $("#dropdownpanel").hide(); 
 
    $("#latin").show(); 
 
}); 
 
    
 
$("#back").click(function() { 
 
    $("#latin").hide(); 
 
    $("#dropdownpanel").show(); 
 
});
#dropdownpanel, #latin { 
 
     width: 236px; 
 
     height: 100vh; 
 
     background: rgba(00, 00, 00, 0.55); 
 
     color: #ffffff; 
 
     position: fixed; 
 
     top: 38px; 
 
     left: 0px; 
 
     display: none; 
 
     padding: 5px; 
 
    }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
 
    <button id="dropdown"> 
 
     Dropdown 
 
    </button> 
 
    
 
    <div id="dropdownpanel"> 
 
     <p>Click 
 
     <a href="#" id="latinlink">here</a> to read some latin... 
 
     </p> 
 
    </div> 
 
    
 
    <div id="latin"> 
 
     <button id="back"> 
 
     Back 
 
     </button> 
 
     <br /> Lorem ipsum dolor sit amet, epicuri argumentum constituam eum ea. Munere aliquip te nec, at nec dicat fuisset offendit, quo an sonet honestatis. Tempor facete sadipscing te his. Nam vivendo postulant reprehendunt te, offendit principes pri ex, at causae 
 
     oblique torquatos pro. 
 
     <br /> 
 
     <br /> Libris philosophia has eu, nam iriure alienum intellegam at. Nec appareat electram ne, amet nonumes per te. Usu eu nisl verterem, labore regione legimus in nam, vel saperet laboramus voluptatibus ut. Ad eam altera nemore. No nobis corpora pro, qui 
 
     ex tale suas dissentias, et iisque omittam inciderint sit. Usu ut case menandri vituperata. 
 
    </div>

另外,你可能會改變將#latin div放入#dropdownpanel的HTML結構。

+0

謝謝,這個伎倆。但是,我注意到了一些新東西。 '#下拉式按鈕只能打開'#dropdownpanel' div。我如何要求它打開先前打開的任何div? – glokul

+0

對不起,我沒有意識到你想要這樣的行爲。我認爲將另一個div放在當前的div上會更簡單一些,並且讓現有的#dropdown按鈕在不更改內部div的可見性的情況下切換新容器div的可見性 - 這種方式無論內部div如何可見仍然可見。您現有的#latinlink和#back點擊處理程序將按原樣運行。 – nnnnnn

0

我爲#dropdownpanel#latin添加了包裝。這裏引用jsfiddle並將樣式從#dropdownpanel更改爲#dropdowncontent

0

問題是該按鈕正在切換不可見的元素。

$("#dropdown").click(function() { 
 
\t if ($('#dropdownpanel').is(':visible')) { 
 
\t \t $("#dropdownpanel").hide(); 
 
\t } else if ($('#latin').is(':visible')) { 
 
\t \t $("#latin").hide(); 
 
\t } else { 
 
\t \t $("#dropdownpanel").show(); 
 
\t } 
 
}); 
 
$("#latinlink").click(function() { 
 
\t $("#dropdownpanel").hide(); 
 
\t $("#latin").show(); 
 
}); 
 
$("#back").click(function() { 
 
\t $("#latin").hide(); 
 
\t $("#dropdownpanel").show(); 
 
});
#dropdownpanel { 
 
    width: 236px; 
 
    height: 100vh; 
 
    background: rgba(00, 00, 00, 0.55); 
 
    color: #ffffff; 
 
    position: fixed; 
 
    top: 38px; 
 
    left: 0px; 
 
    display: none; 
 
    padding: 5px; 
 
} 
 

 
#latin { 
 
    width: 236px; 
 
    height: 100vh; 
 
    background: rgba(00, 00, 00, 0.55); 
 
    color: #ffffff; 
 
    position: fixed; 
 
    top: 38px; 
 
    left: 0px; 
 
    display: none; 
 
    padding: 5px; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 

 
<button id="dropdown">Dropdown</button> 
 
<div id="dropdownpanel"> 
 
    <p>Click <button id="latinlink">here</button> to read some latin...</p> 
 
</div> 
 
<div id="latin"> 
 
    <button id="back">Back</button><br> Lorem ipsum dolor sit amet, modo vitae latine id sea, ea illud facilis definitiones cum. Ei mea ignota iuvaret, debet verear adipiscing no mel, mel gubergren torquatos temporibus ex. Mei nemore mandamus et. Omnes 
 
    legimus principes pri et.<br> 
 
    <br> Nam eu odio facer cotidieque, cum in reque elitr fastidii. An duo hinc iisque principes, nostro mollis laoreet cu ius. No mel habeo soluta repudiare, ex qui tritani apeirian delicatissimi. Eos ei dolore option definitiones, ad sit liber scaevola 
 
    dissentiet. 
 
</div>