Javascript 切换div打开和关闭-确保任何时候只有一个div可见,而不是所有div可见
我使用以下脚本在单击时切换打开和关闭div:Javascript 切换div打开和关闭-确保任何时候只有一个div可见,而不是所有div可见,javascript,jquery,Javascript,Jquery,我使用以下脚本在单击时切换打开和关闭div: (function ($) { $.fn.showHide = function (options) { //default vars for the plugin var defaults = { speed: 1000, easing: '', changeText: 0, showText: 'Show',
(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);
在打开下一个div之前,自动关闭任何当前打开的div的最佳方法是什么(防止在页面上打开所有div-只有单击的链接可见)
HTML格式如下:
<a href="#" class="show_hide" rel="#slidingDiv">View</a><br />
<div id="slidingDiv" style="display:none;">
Fill this space with really interesting content.
</div>
<a href="#" class="show_hide" rel="#slidingDiv_2">View</a><br />
<div id="slidingDiv_2" style="display:none;">
Fill this space with really interesting content.
</div>
用真正有趣的内容填满这个空间。
用真正有趣的内容填满这个空间。
我猜只是在打开之前快速检查一下,但我到目前为止还不能让它工作
非常感谢 向所有滑动div添加一个
slidingDivs
类,只需调用:
$(".slidingDivs").hide();
我认为只隐藏可见的东西是不值得担心的,只需将它们全部隐藏。这可能有用
$(toggleDiv).siblings(".slidingDivs").hide();
$(toggleDiv).toggle();
其中toggle div在代码中定义为
var toggleDiv = $(this).attr('rel');
只需将类“slidingDiv”添加到id为“slidingDiv”和“slidingDiv_2”的div中即可,或者最好添加一个类-openDiv。然后调用$(“.openDiv”).hide();