我找到了解决方案。
将高度设置回 0px 时,似乎只有 jquery 的 .slideUp() 函数会导致问题。只有当浏览器在 QuirksMode (HTML Transitional 4.01 DocType) 和 IE 上运行时,才会出现该错误。我在这里找到的解决方案是 .slideUp() 的替代品。所以而不是
targetElement.slideUp(speed,callBack)
你写
var h = target.height();
var cssHeight=target.css('height');
target.animate({
height: '1px' }, speed, function() {
target.hide();
target.height(h);
target.css('height',cssHeight);
callBack();
});
感谢 Siderite Zackwehdex wo 也向 jQuery ( http://dev.jquery.com/ticket/5062 ) 报告了该错误,但他们不会修复它。他们说:
您只需确保您的文档不处于 quirksmode(提供正确的文档类型)——这是我们在遇到此问题时通常建议的方式。
我还为没有时间或无法控制 HTML 来修复它并使其“QuirksMode Clean”的每个人找到了 .slideToggle() 的修复或替代方法。
在这里,您会找到一个解释和功能,就像一个魅力。我必须做的唯一更改是将高度设置为“1px”,这样如果从一开始就隐藏元素,它就不会在第一次运行 .slideToggle() 时跳开。
所以我的工作解决方案最终看起来像这样:
// this is a fix for the jQuery slide effects
function slideToggle(el, bShow){
var $el = $(el), height = $el.data("originalHeight"), visible = $el.is(":visible");
// if the bShow isn't present, get the current visibility and reverse it
if( arguments.length == 1 ) bShow = !visible;
// if the current visiblilty is the same as the requested state, cancel
if( bShow == visible ) return false;
// get the original height
if( !height ){
// get original height
height = $el.show().height();
// update the height
$el.data("originalHeight", height);
// if the element was hidden, hide it again
if( !visible ) $el.css({height: '1px'}).hide();
}
// expand the knowledge (instead of slideDown/Up, use custom animation which applies fix)
if( bShow ){
$el.show().animate({height: height}, {duration: 500});
} else {
$el.animate({height: '1px'}, {duration: 500, complete:function (){
$el.hide();
}
});
}
}