I want to create a loop so I can walk thru all the CSS properties that are defined in an element. My goal is to create a function that merge the style of two elements.
<span id="a1" style="font-family:verdana;">Hello</span>
<span id="a2" style="font-size:15;">World</span>
And the JS function would look something like this:
function mergeStyle (obj,target){
$.each(test.style.CSSStyleDeclaration,function(){ // This will not work?
// Add to target
})
return;
};
I recommend using classes instead, since your need doesn’t sound as a good design. If you still want it though, this should work:
The
.styleproperty of an element contains its styles;CSSStyleDeclarationis.style‘s constructor which is not element-specific.Usage:
It alters and returns the first element.