六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 150|回复: 0

关于cssText属性

[复制链接]

升级  60.33%

119

主题

119

主题

119

主题

举人

Rank: 3Rank: 3

积分
381
 楼主| 发表于 2013-2-7 23:16:46 | 显示全部楼层 |阅读模式
给一个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)
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表