Suppose, I have ‘td’ style mentioned in an included style sheet called custom.css and also in the style section as in code below, then is there a way in jQuery to get the style mentioned in stylesheet.
<!DOCTYPE html>
<html>
<head>
<link type="text/css" href="../custom.css" rel="stylesheet" />
<style type="text/css">
td {border: 2px solid red;background-color:lightyellow;}
</style>
</head>
In custom.css there is a td style defined as below.
td {border: 1px dashed black;background-color:pink;}
There is no jQuery selector to do this. Javascript accesses the DOM, which doesn’t directly “know” why an element has the computed style that it does. Further and because of this, DOM does not keep track of what the computed style would be without a given style declaration.
That said, one can disable a given style sheet:
http://jsfiddle.net/dPA9u/
You can use this method to turn off a given style sheet, check the computed style of your target element, then re-enable the stylesheet. You’ll end up with the previous style, but the user won’t see anything happen:
Try it: http://jsfiddle.net/GDSEK/1/
Putting it together, you can craft a script that loops through
document.styleSheetsto find the target sheet, disable it, measure the computed style of the target element, then re-enable the target sheet.This does not contend with inline styles — but you can check those too by disabling all the style sheets, then checking on
element.styleDocumentation
document.styleSheetson MDN – https://developer.mozilla.org/en-US/docs/DOM/document.styleSheetsstylesheetobject on MDN – https://developer.mozilla.org/en-US/docs/DOM/stylesheetwindow.getComputedStyleon MDN – https://developer.mozilla.org/en-US/docs/DOM/window.getComputedStyleelement.styleon MDN – https://developer.mozilla.org/en-US/docs/DOM/element.style