<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> *{ margin: 0; padding: 0; list-style: none; } ul{ width: 800px; position: absolute; top: 200px; left: 500px; } ul img{ float: left; border: 1px solid #333; width: 200px; box-sizing: border-box; } </style> </head> <body> <ul> <li><img src="./images/1.jpg" alt=""></li> <li><img src="./images/2.jpg" alt=""></li> <li><img src="./images/3.jpg" alt=""></li> <li><img src="./images/4.jpg" alt=""></li> </ul> <script> // 获取元素 var bodyEle = document.body; var img = document.getElementsByTagName('img'); // 绑定事件 for(var i=0;i<img.length;i++){ img[i].onclick = function(){ // 背景颜色替换 bodyEle.style.background = "url("+this.src+") no-repeat"; } } </script> </body> </html>