现在的位置: 首页 > 综合 > 正文

使用javascript更改某个css class的属性

2013年10月22日 ⁄ 综合 ⁄ 共 625字 ⁄ 字号 评论关闭

使用javascript更改某个css class的属性

问题: 你想要修改某个css class的属性。如:

<style type="text/css">
.orig {
	display: none;
}
</style>

你想要改变把他的display属性由none改为inline。

解决办法: 在IE里:

document.styleSheets[0].rules[0].style.display = "inline";

在firefox里:

document.styleSheets[0].cssRules[0].style.display = "inline";

讨论: 可以做一个函数来搜索特定名字的style对象:

function getstyle(sname) {
	for (var i=0;i<document.styleSheets.length;i++) {
		var rules;
		if (document.styleSheets[i].cssRules) {
			rules = document.styleSheets[i].cssRules;
		} else {
			rules = document.styleSheets[i].rules;
		}
		for (var j=0;j<rules.length;j++) {
			if (rules[j].selectorText == sname) {
				return rules[j].style;
			}
		}
	}
}

然后只要:

getstyle(".orig").display = "inline";

就可以了。

抱歉!评论已关闭.