我有以下的javascript,顯示或隱藏鏈接被點擊時,一個div:jQuery的顯示/隱藏div-小調整
<script type="text/javascript">
$(document).ready(function(){
$('.show_hide').showHide({
speed: 500, // speed you want the toggle to happen
changeText: 0, // if you dont want the button text to change, set this to 0
showText: 'View',// the button text to show when a div is closed
hideText: 'Close' // the button text to show when a div is open
});
});
(function ($) {
$.fn.showHide = function (options) {
//default vars for the plugin
var defaults = {
speed: 1000,
easing: '',
changeText: 0,
showText: 'Show',
hideText: 'Hide',
};
var options = $.extend(defaults, options);
$(this).click(function() {
// this var stores which button you've clicked
var toggleClick = $(this);
// this reads the rel attribute of the button to determine which div id to toggle
var toggleDiv = $(this).attr('rel');
// here we toggle show/hide the correct div at the right speed and using which easing effect
$(toggleDiv).slideToggle(options.speed, options.easing, function() {
// this only fires once the animation is completed
if(options.changeText==1){
$(toggleDiv).is(":visible") ? toggleClick.text(options.hideText) : toggleClick.text(options.showText);
}
});
return false;
});
};
})(jQuery);
</script>
的問題是,我有兩個這樣的div,但我只想要一個一次顯示。因此,如果有人點擊鏈接以顯示div 2
,並且已經顯示div 1
,則應在顯示div2
之前先隱藏自己。
相關HTML:
<div class="button"><a href="#" rel="#faq" class="show_hide">FAQs</a></div>
<div class="button"><a href="#" rel="#contact" class="show_hide">Contact</a></div>
<div id="faq" class="faq">FAQs here </div>
<div id="contact" class="faq">Contact form here </div>
我沒有與JS/jQuery的任何經驗,但我嘗試添加該代碼,沒有工作:
var otherDiv;
if ($(this).attr('rel') == 'contact')
otherDiv = 'faq';
else
otherDiv = 'contact';
if ($(otherDiv).is(":visible"))
$(otherDiv).slideToggle(options.speed);
有點沉重,如果其他jQuery UI組件不會被使用。然而,總體來看,這仍然是一個很好的觀點:目前已有100萬套自給式手風琴;肯定其中一人會工作! –
我嘗試過實施手風琴,看起來效果非常好,但在我的情況下它不適用於我,因爲我的聯繫表格相對較小,而常見問題解答很大。當所有div尺寸大致相同時,手風琴效果最佳。除非我錯過了什麼 – xbonez
這就是我的意思:http://jsfiddle.net/szrhD/3/ – xbonez