您的位置:首页 > Web前端 > JavaScript

javascript操作html元素CSS属性

2014-10-12 15:37 351 查看
以下先记录一下JS控制CSS所使用的方法.

1.使用javascript更改某个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对象:

关于rules和cssRules的浏览器兼容性,本人博文有測试记录:/article/1537445.html

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) {
//selectorText 属性的作用是对一个选择的地址进行替换.意思应该是获取RULES[J]的CLASSNAME.有说错的地方欢迎指正
return rules[j].style;
}
}
}
}


然后仅仅要:

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

就能够了。

------------------ 注意 document.styleSheets[0].rules[0].style 这个 styleSheets[0]数组的下标是代表本页的第N个CSS样式表,它的下级rules[0]的数组下标表示的则是这个样式表中的第N个样式,比如:

<style type="text/css">

.s{display="none";}

.w{display="none";}

</style>

改动S则: document.styleSheets[0].rules[0].style.display='inline';

改动W则:document.styleSheets[0].rules[1].style.display = 'inline';

注意:CSS和HTML结合的方式必须为<LINK rel="stylesheet" type="text/css" href="" /> 或<style></style>的时候以上方法可行,如@IMPORT 则不行.

====================================

以下记录一下JS訪问CSS中的样式:

用javascript获取和设置style

DOM标准引入了覆盖样式表的概念,当我们用document.getElementById("id").style.backgroundColor 获取样式时 获取的仅仅是id中style属性中设置的背景色,假设id中的style属性中没有设置background-color那么就会返回空,也就是说假设id用class属性引用了一个外部样式表,在这个外部样式表中设置的背景色,那么不好意思

document.getElementById("id").style.backgroundColor 这样的写法不好使,假设要获取外部样式表中的设置,须要用到window对象的getComputedStyle()方法,代码这样写window.getComputedStyle(id,null).backgroundColor

可是兼容问题又来了,这么写在firefox中好使,但在IE中不好使

两者兼容的方式写成

window.getComputedStyle?window.getComputedStyle(id,null).backgroundColor:id.currentStyle["backgroundColor"];

假设是获取背景色,这样的方法在firefox和IE中的返回值还是不一样的,IE中是返回"#ffff99"样子的,而firefox中返回"rgb(238, 44, 34) "

值得注意的是:window.getComputedStyle(id,null)这样的方式不能设置样式,仅仅能获取,要设置还得写成类似这样id.style.background="#EE2C21";

在IE中CURRENTSTYLE仅仅能以仅仅读方式获取样式.

用JavaScript改动CSS属性

仅仅有写原生的javascript了。

1.用JS改动标签的 class 属性值:

class 属性是在标签上引用样式表的方法之中的一个,它的值是一个样式表的选择符,假设改变了 class 属性的值,标签所引用的样式表也就更换了,所以这属于第一种改动方法。

更改一个标签的 class 属性的代码是:

document.getElementById( id ).className = 字符串;

document.getElementById( id ) 用于获取标签相应的 DOM 对象,你也能够用其他方法获取。className 是 DOM 对象的一个属性,它相应于标签的 class 属性。字符串 是 class 属性的新值,它应该是一个已定义的CSS选择符。

利用这样的办法能够把标签的CSS样式表替换成另外一个,也能够让一个没有应用CSS样式的标签应用指定的样式。

举例:

<style type="text/css">
.txt {
font-size: 30px; font-weight: bold; color: red;
}
</style>
<div id="tt">欢迎光临!</div>
<p><button onclick="setClass()">更改样式</button></p>
<script type="text/javascript">
function setClass()
{
document.getElementById( "tt" ).className = "txt";
}
</script>


2.用JS改动标签的 style 属性值:

style 属性也是在标签上引用样式表的方法之中的一个,它的值是一个CSS样式表。DOM 对象也有 style 属性,只是这个属性本身也是一个对象,Style 对象的属性和 CSS 属性是一一相应的,当改变了 Style 对象的属性时,相应标签的 CSS 属性值也就改变了,所以这属于另外一种改动方法。

更改一个标签的 CSS 属性的代码是:

<div id="t2">欢迎光临!</div>
<p><button onclick="setSize()">大小</button>
<button onclick="setColor()">颜色</button>
<button onclick="setbgColor()">背景</button>
<button onclick="setBd()">边框</button>
</p>
<script type="text/javascript">
function setSize()
{
document.getElementById( "t2" ).style.fontSize = "30px";
}
function setColor()
{
document.getElementById( "t2" ).style.color = "red";
}
function setbgColor()
{
document.getElementById( "t2" ).style.backgroundColor = "blue";
}
function setBd()
{
document.getElementById( "t2" ).style.border = "3px solid #FA8072";
}
</script>


document.getElementById( id ).style.属性名 = 值;

document.getElementById( id ) 用于获取标签相应的 DOM 对象,你也能够用其他方法获取。style 是 DOM 对象的一个属性,它本身也是一个对象。属性名 是 Style 对象的属性名,它和某个CSS属性是相相应的。

说明:这样的方法改动的单一的一个CSS属性,它不影响标签上其他CSS属性值。

举例:
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: