<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<div id="div1" style="width:200px; height:200px; background:red"></div>
</body>
<script>
var oDiv = document.getElementById('div1')
function css(obj,name,value) {
if (arguments.length == 2) {
// 获取样式
return obj.style[name];
}
else {
// 设置样式
obj.style[name] = value;
}
};
alert(css(oDiv, 'width'))
css(oDiv, 'background' ,'yellow')
</script>
</html>
获取样式.png
设置样式.png
网友评论