jQuery在隐藏的父母中显示孩子,将显示属性设置不正确



请参阅以下JSFIDDLE进行演示:

http://jsfiddle.net/drb8s/

当您在隐藏父母内的元素上使用jQuery show()时,问题似乎会发生。(然后随后显示父母)。如果显示子元素:最初是表格,当您调用show()时,它将其切换回显示:块不正确。

我只能使用小提琴中的控制台对此进行复制,但是在我的应用程序中,我会通过代码体验此问题。

html:

<div class="outer">
    <div class="inner">Inner</div>
</div>

CSS:

.outer {
    display: none;
}
.inner {
    display: table;
}​

JS:

$('.inner').hide();
// $('.inner').show();​​​  // this works in jsfiddle (but not in my app, or run through console)

编辑:作为解决方法,我正在使用.css('display', 'table'),但我只是想知道是否有人知道此的实际原因。

.show.hide在其显示/隐藏的方法中是残酷的;如果您想要特定的非传统样式,则不能使用它们:

$(".inner").data('oldstyle', $(".inner").css('display')).css('display', 'none');
...
$(".inner").css('display', $(".inner").data('oldstyle'));

最新更新