Quello che hai dovrebbe funzionare già.
In jQuery, a meno che l'attributo di stile dell'elemento ha display
inizialmente impostata su qualcosa di diverso da none
, quando il show
si chiama, non fa altro che rimuovere l'attributo di stile per la visualizzazione. Non lo blocca.
È possibile vedere da soli in this fiddle che quando si fa clic sul pulsante, lo display
impostato nel CSS è l'elemento su cui è impostato l'elemento.
Ecco il codice rilevante nel jQuery source:
function showHide(elements, show) {
var display, elem, hidden,
values = [],
index = 0,
length = elements.length;
for (; index < length; index++) {
elem = elements[ index ];
if (!elem.style) {
continue;
}
values[ index ] = data_priv.get(elem, "olddisplay");
display = elem.style.display;
if (show) {
// Reset the inline display of this element to learn if it is
// being hidden by cascaded rules or not
if (!values[ index ] && display === "none") {
elem.style.display = "";
}
// Set elements which have been overridden with display: none
// in a stylesheet to whatever the default browser style is
// for such an element
if (elem.style.display === "" && isHidden(elem)) {
values[ index ] = data_priv.access(elem, "olddisplay", css_defaultDisplay(elem.nodeName));
}
} else {
if (!values[ index ]) {
hidden = isHidden(elem);
if (display && display !== "none" || !hidden) {
data_priv.set(elem, "olddisplay", hidden ? display : jQuery.css(elem, "display"));
}
}
}
}
// Set the display of most of the elements in a second loop
// to avoid the constant reflow
for (index = 0; index < length; index++) {
elem = elements[ index ];
if (!elem.style) {
continue;
}
if (!show || elem.style.display === "none" || elem.style.display === "") {
elem.style.display = show ? values[ index ] || "" : "none";
}
}
return elements;
}
Vorrei sottolineare che le classi non girevoli di solito essere più veloce, in generale, perché c'è meno informazioni per verificare.
Si potrebbe attivare un nome di classe che cambia tra la 'visualizzazione: none' e' display: table-cell' –