|
给一个html元素设置css属性,如:
var head= document.getElementById("head");head.style.width = "200px";head.style.height = "70px";head.style.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属性,后在各浏览器中测试都通过了。一行代码即可,实在很妙。如下:
var head= document.getElementById("head");head.style.cssText="width:200px;height:70px;display:bolck";
测试浏览器版本如下(与浏览器模式无关):
<span class="Apple-style-span" style="border-collapse: separate; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; text-indent: 0px; white-space: normal; font-family: Simsun; color: #000000; font-size: medium;"><span class="Apple-style-span" style="font-size: 13px; font-family: Verdana;">IE 6/7
IE 8 (Emulate IE7)<div style="margin-top: 0px; margin-bottom: 0px;">IE 8 (Enforce IE8) |
|