javascript - 仅使用 CSS 关闭模式

标签 javascript html css modal-dialog

我需要像这样使用 css 来关闭我的模式:http://jsfiddle.net/raving/1mhsynmw/

但是,我无法让它工作。我的模式如下。

function alert(msg) {
	document.getElementById("alert").innerHTML = '<div class="modal-overlay"><div class="modal"><div class="modal-container"><h3>' + msg + '</h3><p>' + msg + '</p></div><p class="modal-footer"><a href="javascript:" id="ok">OK</a></p></div>';
}

alert("This is some text to show you this modal. This is some text to show you this modal. This is some text to show you this modal. This is some text to show you this modal.");
body {
  font-family:arial;
  font-size:11px;
}
.modal {
     position: fixed;
     top: 20px;
     margin-left: auto;
     margin-right: auto;
     left: 0;
     right: 0;
     width: 200px;
     box-shadow: 0 4px 6px 1px rgba(50, 50, 50, 0.14);
     background: #fff;
}
.modal p {
	 cursor:default;
}
.modal-container {
	 padding: 10px;
}
.modal p a {
	 color:#555;
}
.modal-footer a {
	 display:block;
	 border:1px solid #eee;
	 width:9.5%;
	 padding:3px;
	 background:#fff;
	 text-decoration:none;
	 float:right;
}
.modal-footer {
     background: #fafafa;
     border-top: 1px solid #eee;
     margin-bottom: 0px;
	 margin-top:0px;
	 padding:8px;
	 height:25px;
}
.modal h3 {
	 margin:0px;
	 white-space: nowrap;
     overflow: hidden;
     text-overflow: ellipsis;
     max-width: 175px;
}
.modal-last {
	 padding-bottom:0px;
}
.modal-overlay:before {
     content: '';
     position: fixed;
     top: 0px;
     left: 0px;
     width: 100%;
     height: 100%;
     background: rgba(0, 0, 0, 0.5);
}
<div id="alert"></div>

<h3>Content..</h3>
<p>Just to show it's a modal</p>

问题:有什么方法可以在不使用 Javascript 的情况下关闭模式?这是不可能的,我如何使用 Javascript 关闭此模式?

最佳答案

如果您不想在单击“确定”按钮时使用 JQuery 关闭模式,您可以使用纯 JavaScript 执行类似于以下代码的操作。您必须为选择器分配一个索引才能使其正常工作。

    //begin click event
    document.getElementById("ok").addEventListener("click",function(event){


    /* Hide the element with a class name of modal.
       note:If there is more than one element with the class name
       modal then this code will only select the first one in the
       collection of elements 
    */

    document.getElementsByClassName("modal")[0].style.display = "none";


       });//end click event

关于javascript - 仅使用 CSS 关闭模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35230393/

相关文章:

javascript - jQuery 菜单无法正常工作

javascript - 在 PHP 中提取缩略图

javascript - 缓存动态加载的图像

css - 我的 HTML5 和 CSS 标题横幅不工作

CSS:为 div 内的输入元素定义样式

css - 如何防止子 div 超出父 div 的高度?

javascript - 导致钩子(Hook)对象更新重新渲染的最佳方法

javascript - 如何更改此 "snow script"中雪的颜色

javascript - Google Apps 脚本中的 Dropbox API v2/删除

javascript - 选择 li 项时更改无序列表的值