语法:Object.style.property=new style;
注意:Object是获取的元素对象,如通过document.getElementById("id")获取的元素。
基本属性表(property):
属性 |
描述 |
backgroundColor |
设置元素的背景色 |
height |
设置元素的高度 |
width |
设置元素的宽度 |
color |
设置文本的颜色 |
font |
在一行设置所有的字体属性 |
fontFamily |
设置元素的字体系列 |
fontSize |
设置元素的字体大小 |
注意:该表只是一小部分CSS样式属性,其它样式也可以通过该方法设置和修改。
看看下面的代码:改变<p> 元素的样式
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<p id='con'>对于无法保证的明天,内心惶恐惶惶不安</p>
<script type="text/javascript">
var mycon = document.getElementById('con');
mycon.style.color = 'white';
mycon.style.width = '300px';
mycon.style.height = '300px';
mycon.style.backgroundColor = '#f90';
mycon.style.lineHeight = '300px';
mycon.style.fontSize = '14px';
mycon.style.textAlign ='center';
</script>
</body>
</html>
发表评论