我正在制作 Accordion 式效果,使用切换来扩展和收缩 div 的高度。
当用户切换 div 时,它的高度会扩展,我希望前一个 div 也能切换回来。 因此,我需要选择所有同级,但我只想定位已展开的 div,以再次缩小其高度。
我想使用一个函数来选择扩展的 div,如果高度超过 99 px,我认为这是仅选择扩展的 div 的最佳方法。
我哪里出错了?
我的代码。
$(function() {
jQuery.fn.selectOpen = (function(){
//(this).css('background-color', 'red');
if ( $(this).height() > 99) {
$(this).trigger(".toggle");
}
});
});
$("#arrow_01").toggle(function(){
$("#arrow_01").attr('src','images/arrow_down.png');
$("#expanding_box_01").animate({height: '100px', }).siblings().selectOpen();
}, function(){
$('#arrow_01').attr('src','images/arrow_right.png');
$("#expanding_box_01").animate( {height: '27px' });
});
最佳答案
使用.each()
遍历所有同级,并且您错误地使用.toggle
作为事件名称,而它应该只是toggle
,像这样:
jQuery.fn.selectOpen = function(){
//(this).css('background-color', 'red');
this.each(function() {
if ( $(this).height() > 99) {
$(this).trigger("toggle");
}
});
};
关于jquery - .toggle() 所有高度超过 99px 的 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12786228/