<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> .content{ height: 1800px; background-color: white; } .shade{ position: fixed; top:0; left: 0; right: 0; bottom: 0; background-color: black; opacity: 0.3; } .model{ 200px; height:200px; background-color: white; position: absolute; top:50%; left: 50%; margin-top: -100px; margin-left: -100px; } .hide{ display: none; } </style> </head> <body> <div class="content"> <button onclick="func()">show</button> </div> <div class="shade hide"> </div> <div class="model hide"> <button onclick="cancle()">取消</button> </div> <script> function func() { var ele_shade = document.getElementsByClassName('shade')[0] var ele_model = document.getElementsByClassName('model')[0] ele_shade.classList.remove('hide') //class属性的删除 ele_model.classList.remove('hide') } function cancle() { var ele_shade = document.getElementsByClassName('shade')[0] var ele_model = document.getElementsByClassName('model')[0] ele_shade.classList.add('hide')//class属性的增加 ele_model.classList.add('hide') } </script> </body> </html>
class属性
1)className
2)classList