<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<title>Document</title>
<style type="text/css" media="screen">
#canvas{
width: 100%;
height: 500px;
}
ul li{
list-style: none;
float: left;
}
li:nth-of-type(1){
background: red;
color: white;
}
li:nth-of-type(2){
background: green;
color: white;
}
li:nth-of-type(3){
background: blue;
color: white;
}
</style>
</head>
<body>
<div id="canvas"></div>
<ul>
<li>红色</li>
<li>绿色</li>
<li>蓝色</li>
</ul>
<script type="text/javascript">
var canvas = document.getElementById("canvas");
var ul = document.getElementsByTagName("ul")[0];
ul.addEventListener('click',function(event){
var that = event.target;
canvas.style.background = getStyle(that,'background');
})
//读取样式表数据
function getStyle(obj,name){
return window.getComputedStyle?getComputedStyle(obj,null)[name]:obj.currentStyle[name];
}
</script>
</body>
</html>