如何显示非模态/非模态 HTML/JavaScript 对话

我根据 w3schools 上的示例创建了一个模态对话。但我一直无法找到有关如何使其非模态的指导。即是否可以显示对话并且我仍然可以在后台按链接?


实际上,我想要实现的是允许对话框内的链接可单击,并允许其后面主页上的 JavaScript 对这些事件做出反应。


<!DOCTYPE html>

<html>

    <head>

        <meta charset="UTF-8">

        <meta http-equiv="Content-Security-Policy"  content="connect-src * 'unsafe-inline';">

        <style>

/* The Modal (background) */

.modal {

  display:block; /* Hidden by default */

  position: fixed; /* Stay in place */

  z-index: 1; /* Sit on top */

  left: 0;

  top: 0;

  width: 100%; /* Full width */

  height: 100%; /* Full height */

  overflow-y: auto; /* Enable scroll if needed */

  background-color: rgb(0,0,0); /* Fallback color */

  background-color: rgba(0,0,0,0.4); /* Black w/ opacity */

}


/* Modal Content/Box */

.modal-content {

  background-color: #fefefe;

  margin: 15% auto; /* 15% from the top and centered */

  padding: 20px;

  border: 1px solid #888;

  width: 80%; /* Could be more or less, depending on screen size */

}


/* The Close Button */

.close {

  color: #aaa;

  float: right;

  font-size: 28px;

  font-weight: bold;

}


.close:hover,

.close:focus {

  color: black;

  text-decoration: none;

  cursor: pointer;

    </style>

    </head>


<body>

    <a href='#'>Press me press me I am blocked</a>

    <!-- The Modal -->

    <div id="myModal" class="modal">

        <!-- Modal content -->

        <div class="modal-content">

        <span class="close" onclick="modalHide()">&times;</span>

        <div id="modtext">Some text in the Modal..</div>

        </div>

    </div>

    <script>

    function modalHide(){

        document.getElementById("myModal").style.display = "none";

    }

    </script>

</body>


</html>


料青山看我应如是
浏览 72回答 1
1回答

跃然一笑

只需删除(.modal)模态背景,因为它作为覆盖层工作。/* The Modal (background) */.modal {&nbsp; display:block; /* Hidden by default */&nbsp; position: fixed; /* Stay in place */&nbsp; z-index: 1; /* Sit on top */&nbsp; left: 0;&nbsp; top: 0;&nbsp; width: 100%; /* Full width */&nbsp; height: 100%; /* Full height */&nbsp; overflow-y: auto; /* Enable scroll if needed */&nbsp; background-color: rgb(0,0,0); /* Fallback color */&nbsp; background-color: rgba(0,0,0,0.4); /* Black w/ opacity */}上面的代码创建了一个覆盖层并显示其中的对话框。如果删除它,对话框后面的链接将起作用。<!DOCTYPE html><html>&nbsp; &nbsp; <head>&nbsp; &nbsp; &nbsp; &nbsp; <meta charset="UTF-8">&nbsp; &nbsp; &nbsp; &nbsp; <meta http-equiv="Content-Security-Policy"&nbsp; content="connect-src * 'unsafe-inline';">&nbsp; &nbsp; &nbsp; &nbsp; <style>/* Modal Content/Box */.modal-content {&nbsp; background-color: #fefefe;&nbsp; margin: 15% auto; /* 15% from the top and centered */&nbsp; padding: 20px;&nbsp; border: 1px solid #888;&nbsp; width: 80%; /* Could be more or less, depending on screen size */}/* The Close Button */.close {&nbsp; color: #aaa;&nbsp; float: right;&nbsp; font-size: 28px;&nbsp; font-weight: bold;}.close:hover,.close:focus {&nbsp; color: black;&nbsp; text-decoration: none;&nbsp; cursor: pointer;}&nbsp;&nbsp; &nbsp; </style>&nbsp; &nbsp; </head><body>&nbsp; &nbsp; <a href='#'>Press me press me I am blocked</a>&nbsp; &nbsp; <!-- The Modal -->&nbsp; &nbsp; <div id="myModal" class="modal">&nbsp; &nbsp; &nbsp; &nbsp; <!-- Modal content -->&nbsp; &nbsp; &nbsp; &nbsp; <div class="modal-content">&nbsp; &nbsp; &nbsp; &nbsp; <span class="close" onclick="modalHide()">&times;</span>&nbsp; &nbsp; &nbsp; &nbsp; <div id="modtext">Some text in the Modal..</div>&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; </div>&nbsp; &nbsp; <script>&nbsp; &nbsp; function modalHide(){&nbsp; &nbsp; &nbsp; &nbsp; document.getElementById("myModal").style.display = "none";&nbsp; &nbsp; }&nbsp; &nbsp; </script></body></html>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Html5