|
给一个html元素设置css属性,如:
Js代码
var head= document.getElementById("head");
head.style.width = "200px";
head.style.height = "70px";
head.style.display = "block";
var head= document.getElementById("head");
head.style.width = "200px";
head.style.height = "70px";
head.style.display = "block";
这样写太罗嗦了,为了简单些写个工具函数,如:
Js代码
function setStyle(obj,css){
for(var atr in css)
obj.style[atr] = css[atr];
}
var head= document.getElementById("head");
setStyle(head,{width:"200px",height:"70px",display:"block"})
function setStyle(obj,css){
for(var atr in css)
obj.style[atr] = css[atr];
}
var head= document.getElementById("head");
setStyle(head,{width:"200px",height:"70px",display:"block"})
偶尔发现google api 中使用了cssText属性,后在各浏览器中测试都通过了。一行代码即可,实在很妙。如下:
Js代码
var head= document.getElementById("head");
head.style.cssText="width:200px;height:70px;display:bolck";
var head= document.getElementById("head");
head.style.cssText="width:200px;height:70px;display:bolck";
测试浏览器版本如下(与浏览器模式无关):
IE 6/7
IE 8 (Emulate IE7)
IE 8 (Enforce IE8)
Opera 10.10
Firefox 3.5.6
Safari 4.0.3
Chrome 4.0.266.0
|
|