function findStyle(selectorText) { for (var i = 0; i < document.styleSheets.length; ++i) { var styleSheet = document.styleSheets[i]; var css = styleSheet.cssRules || styleSheet.rules; for (var j = 0; j < css.length; ++j) { var rule = css[j]; if (rule.selectorText == selectorText) return rule.style; } } }
Die Funktion kann dann wie folgt verwendet werden:
findStyle('.test').width = '100px';
<input onkeypress="alert((event || window.event).keyCode);">
<div tabindex="0" onkeypress="alert((event || window.event).keyCode);" onmouseover="focus();" onmouseout="blur();"> TEST </div>
function addHeader(text) { var head = document.getElementsByTagName("head")[0]; var div = document.createElement('div'); div.innerHTML = text; for (var i = 0; i < div.childNodes.length; ++i) { var element = document.createElement(div.childNodes[i].nodeName); element.innerHTML = div.childNodes[i].innerHTML; for (var j = 0; j < div.childNodes[i].attributes.length; ++j) { element.setAttribute( div.childNodes[i].attributes[j].name, div.childNodes[i].attributes[j].value); } head.appendChild(element); } }