<!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>显示和隐藏图片</title> </head> <body> <div> <img src="tao.png" > <button class="close-btn">点我</button> </div> <script> //获取元素 var img = document.querySelector(‘img‘); var btn = document.querySelector(‘.close-btn‘); var flag = 0; //注册事件 程序处理 btn.onclick = function(){ if(flag == 0){ img.style.display = "none"; flag = 1; }else{ img.style.display = "block"; flag = 0; } } </script> </body> </html>