Những gì bạn phải làm việc.
Trong jQuery, trừ khi thuộc tính kiểu của phần tử có display
ban đầu được đặt thành một thứ khác không phải là none
, khi gọi show
, tất cả điều đó sẽ loại bỏ thuộc tính kiểu hiển thị. Nó không đặt nó chặn.
Bạn có thể tự mình xem trong this fiddle khi nhấn nút, display
được đặt trong CSS là phần tử được đặt thành.
Dưới đây là các mã có liên quan trong 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;
}
Tôi nên lưu ý rằng các lớp học chuyển đổi qua lại thường sẽ nhanh hơn nói chung vì có ít thông tin để kiểm tra.
Bạn có thể chuyển đổi một tên lớp mà thay đổi nó giữa 'display: none' và' display: table-cell' –