2.4点击按钮填色

功能:点击第一个按钮红色,第二个绿色

事件:onclick

 属性:className,利用赋值class名把原本css里类的样式附加给div

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>Examples</title>
<meta name="description" content="">
<meta name="keywords" content="">

<link href="css1.css" rel="stylesheet" type="text/css"
charset="UTF-8">
</head>
<body>
<input id="btn1" type="button" value="红色"/>
<input id="btn2" type="button" value="绿色"/>
<div id="div1"></div>
<script src="js1.js"> </script>
</body>
</html>

/////////////////css

#div1{
width:200px;
height:200px;
border:1px #ccc solid;
}
.boxred{
background:red;
}
.boxgreen{
background:green;
}

////////////////////js

var oBtn1=document.getElementById("btn1");
var oBtn2=document.getElementById("btn2");
oBtn1.onclick=function(){
var oDiv=document.getElementById("div1");
oDiv.className="boxred";
};
oBtn2.onclick=function(){
var oDiv=document.getElementById("div1");
oDiv.className="boxgreen";
};

 

posted @ 2018-03-04 14:08  萹豆  阅读(168)  评论(0编辑  收藏  举报