Evening!
For my basic JavaScript library, I've gotten kind of stumped looking at how to get and set the absolute DEFAULT CSS properties--like in jQuery.
For example, after looking at the jQuery source, I can see that for the show(), hide(), and toggle() methods is that there is a function, showHide() (snippet at the bottom), that accesses a method called, "._data" to retrieve the "olddisplay" of the element(s).
I only know of window.getComputedStyle, which changes when an element has a style applied to it. And if the original style of a <div> was "display:none", how can you get the old display when "display:none" would be the oldest record of CSS?
Are "cascaded rules" those original styles by any chance?
There is also a function in the code snipped called, css_defaultDisplay() which has something to do with an iframe? Is there any easier way to do all of this?
Thanks for the replies everybody.
Edit: Couldn't I just make a new element from the tagName of the element I want to get the style of and get IT's computed style?
Code snippet from jQuery 1.10.2 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 ] = jQuery._data( 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 ] = jQuery._data( elem, "olddisplay", css_defaultDisplay(elem.nodeName) );
}
} else {
if ( !values[ index ] ) {
hidden = isHidden( elem );
if ( display && display !== "none" || !hidden ) {
jQuery._data( 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;
}