JS简单的更改样式

来源:互联网 发布:ssh权限管理系统源码 编辑:程序博客网 时间:2024/05/22 04:26

JS学习笔记

用简单的JS函数更改div的样式。

<!DOCTYPE html>

<html lang="zh-cn">
<head>
<meta charset="UTF-8">
<title>JS更改样式</title>
<style>
#div1 {width: 100px;height: 100px;background: red;}
</style>
<script>
function setStyle (name,value){
var odiv=document.getElementById('div1');
odiv.style[name]=value;
}


</script>
</head>
<body>
<input type="button" value="变宽" onclick="setStyle('width','200px')">
<input type="button" value="变高" onclick="setStyle('height','200px')">
<input type="button" value="变绿" onclick="setStyle('background','green')">
<div id="div1"></div>
</body>
</html>
原创粉丝点击