日期:2014-05-17 浏览次数:20538 次
el.style.backgroundColor = "blue";
el.style.backgroundColor = "blue"; el.style.border = "1px solid black"; el.style.margin = "10px";
el.style.cssText = "background-color:blue;border:1px solid black;margin:10px;";
<html> <head> <title> Computed Styles Example </title> <style type='text/css'> #myDiv { background-color: blue; width: 100px; height: 200px; } </style> </head> <body> <div id='myDiv' style='background-color: red; border: 1px solid black'> </div> </body> </html>
var myDiv = document.getElementById('myDiv'); var computedStyle = document.defaultView.getComputedStyle(myDiv, null); alert(computedStyle.backgroundColor); //rgb(255,0,0) or #ff0000 or red alert(computedStyle.width); //”100px” alert(computedStyle.height); //”200px” alert(computedStyle.border); //”1px solid black” in some browsers alert(computedStyle.borderLeftWidth); //”1px" alert(computedStyle.borderLeftStyle); //”solid"
var style = document.getElementById('myDiv').currentStyle; alert(style.borderLeftStyle);//'solid' alert(style.paddingLeft);//'0px' alert(style.backgroundColor);//'red'