Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to get default CSS properties like jQuery?
    primarykey
    data
    text
    <p>Evening!</p> <p>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.</p> <p>For example, after looking at the jQuery source, I can see that for the <strong>show()</strong>, <strong>hide()</strong>, and <strong>toggle()</strong> methods is that there is a function, <strong>showHide()</strong> (snippet at the bottom), that accesses a method called, "._data" to retrieve the "olddisplay" of the element(s). </p> <p>I only know of <strong>window.getComputedStyle</strong>, which changes when an element has a style applied to it. And if the original style of a &lt;div&gt; was "display:none", how can you get the old display when "display:none" would be the oldest record of CSS?</p> <p>Are "cascaded rules" those original styles by any chance?</p> <p>There is also a function in the code snipped called, <strong>css_defaultDisplay()</strong> which has something to do with an iframe? Is there any easier way to do all of this?</p> <p>Thanks for the replies everybody.</p> <p><strong>Edit:</strong> 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?</p> <p>Code snippet from jQuery 1.10.2 source:</p> <pre><code>function showHide( elements, show ) { var display, elem, hidden, values = [], index = 0, length = elements.length; for ( ; index &lt; 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 ] &amp;&amp; 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 === "" &amp;&amp; isHidden( elem ) ) { values[ index ] = jQuery._data( elem, "olddisplay", css_defaultDisplay(elem.nodeName) ); } } else { if ( !values[ index ] ) { hidden = isHidden( elem ); if ( display &amp;&amp; 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 &lt; 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; </code></pre> <p>}</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload