<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
<style>
* {
margin: 0px;
padding: 0px;
}
body {
background-image: url("images/1.jpg");
}
#mask {
background-color: rgba(255, 255, 255, 0.3);
height: 200px;
text-align: center;
}
#mask img {
width: 200px;
margin-top: 35px;
cursor: pointer;
}
</style>
</head>
<body id="bd">
<div id="mask">
<img src="images/1.jpg" alt="">
<img src="images/2.jpg" alt="">
<img src="images/3.jpg" alt="">
</div>
<script src="common.js"></script>
<script>
var imgObjs=my$("mask").children;//获取的所有的子元素
//循环遍历所有img,注册点击事件
for(var i=0;i<imgObjs.length;i++){
imgObjs[i].onclick=function () {
document.body.style.backgroundImage="url("+this.src+")";
};
}
</script>
</body>
</html>
common.js
/**
* 根据id属性的值,返回对应的标签元素
* @param id id属性的值,string类型的
* @returns {Element} 元素对象
*/
function my$(id) {
return document.getElementById(id);
}