温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

JavaScript中怎么实现一个模态对话框

发布时间:2021-07-06 16:53:08 来源:亿速云 阅读:80 作者:Leah 栏目:开发技术

这篇文章给大家介绍JavaScript中怎么实现一个模态对话框,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

<!DOCTYPE html><html lang="en"><head>  <meta charset="UTF-8">  <title>dialogue</title>  <style>    *{      margin: 0;    }    .hide{      display: none;    }    #p1{      height: 2000px;      background-color: #b4b4b4;      position: fixed;      width: 100%;      top: 0;      left: 0;    }    #p2{      /*display: none;*/      background-color: red;      opacity: 0.1;      position: fixed;      width: 100%;      top: 0;      left: 0;      right: 0;      bottom: 0;    }    #p3{      /*display: none;*/      height: 200px;      width: 200px;      z-index: 1002;      background-color: crimson;      position: absolute;      top: 50%;      left:50%;      margin-left: -100px;      margin-top: -100px;    }  </style></head><body><p id="p1">  <input type="button" value="click" onclick="show()"></p><p id="p2" class="p hide"></p><p id="p3" class="p hide">  <input type="button" value="cancel" onclick="cancel()"></p><script>  function show() {    var ele = document.getElementsByClassName("p");    for (var i = 0; i < ele.length; i++) {      ele[i].classList.remove("hide");      console.log(i);    }  }   function cancel(){      var ele=document.getElementsByClassName("p");      for (var i=0;i<ele.length;i++){        ele[i].classList.add(("hide"));      }   }</script></body></html>

初始页面如下:

点击"click"后显示如下:

点击"cancel"后再回到初始画面.

这段代码模拟了模态对话框的实现过程.

关于JavaScript中怎么实现一个模态对话框就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI