我相信这样做的唯一方法是将样式添加为带有’!important’后缀的新CSS声明。最简单的方法是将新的<style>
元素附加到文档的开头:
function addNewStyle(newStyle) {
var styleElement = document.getElementById('styles_js');
if (!styleElement) {
styleElement = document.createElement('style');
styleElement.type = 'text/css';
styleElement.id = 'styles_js';
document.getElementsByTagName('head')[0].appendChild(styleElement);
}
styleElement.appendChild(document.createTextNode(newStyle));
}
addNewStyle('td.EvenRow a {display:inline !important;}')
使用上述方法添加的规则(如果使用!important后缀)将覆盖其他先前设置的样式。如果您不使用后缀,请确保考虑“特异”之类的概念。
你可以使用几个简单的单线执行此操作。
1)在元素上设置“样式” 属性:
element.setAttribute('style', 'display:inline !important');
要么…
element.style.cssText = 'display:inline !important';
两者都会做。
===
顺便说一句-如果你想要一个有用的工具来操作!important
元素中的规则,