I updated the answer to be more efficient, while also providing a working demo ...
$(function() { // element tag example p and element id function get_element_style(element, id){ var css = {}; $('<iframe id="get-style-'+id+'" style="display:none"/>').appendTo('body'); $('#get-style-'+id).contents().find('body').append('</'+element+'>'); $el = $('#get-style-'+id).contents().find('body').find(element); var defaults_css = $el.getStyles(); $('#get-style-'+id).remove(); var element_css = $('#'+id).getStyles(); for (var i in element_css) { if (element_css[i] !== defaults_css[i]) { css[i] = element_css[i]; } } return css; } var properties = get_element_style('p', 'test-p'); });
The problem with getting the style of an element is that you are not getting only the set value, as well as the default values. with this code, I am trying to get only the set values โโof an element. this work with both inline and <style> and <link>
Luca filosofi
source share