我为模式编写并复制了一小段JavaScript,并将其添加到我的main.js文件中,但这似乎无效。
它可以在jsfiddle上运行,但不能在我的机器上运行:https://jsfiddle.net/281wr3c6/13/
由于某种原因它似乎不在我的计算机上
var modal = document.getElementById("theModal");
var btn = document.getElementById("trigger");
var span = document.getElementsByClassName("close")[0];
btn.onclick = function() {
modal.style.display = "block";
}
div.onclick = function() {
modal.display.style = "none";
}
window.onclick = function(event) {
if (event.target == modal) {
modal.display.style = "none";
}
}
.modal {
display: none;
position: fixed;
z-index: 1;
left: 0;
top: 0;
width: 100%;
height: 100%;
overflow: auto;
background: rgba(11,12,12,0.66);
}
.modal.bg {
padding:30px;
float: left;
z-index: 0;
border: none;
border-radius: 0;
margin: 18px 0;
}
.modal .card {
border: 4px solid #dee0e2;
padding:24px;
float: left;
margin: 18px 0;
z-index: 10;
background: #fff;
}
.modal .title {
width: 90%;
display: inline-block;
}
.modal h3 {
margin-top:0;
}
.modal div.buttons button.btn {
margin-bottom: 0!important;
}
.modal div.buttons button.btn.secondary {
margin-left: 0!important;
float: left;
}
.modal div.buttons button.btn.primary {
margin-right: 0!important;
float: right;
}
.modal div.close {
background-image: url("../assets/icons/close.svg");
display: inline-block;
height: 18px;
width: 18px;
padding: 3px;
float: right;
}
<button id="trigger" class="btn primary">Open modal</button>
<div id="theModal" class="cd-box modal bg">
<div class=card>
<div class="title">
<h3>Title</h3>
</div>
<div class="close"></div>
<div class="inside">
<p>This is some content that goes into the modal. The width adjusts according to the content.</p>
</div>
<div class="buttons">
<button class="btn secondary">Exit</button>
<button class="btn primary">Continue with action</button>
</div>
</div>
</div>
我从这里获得了代码:https://www.w3schools.com/howto/howto_css_modals.asp
当我在他们的小提琴/预览中运行代码时,它确实起作用了,所以使我认为这是我的网站未运行的原因?
答案 0 :(得分:1)
要使其正常工作,您需要做一些更改。
首先像詹姆斯·怀特利(James Whiteley)一样,您的div变量未定义。所以这行不通:
div.onclick = function() {
modal.display.style = "none";
}
其次,您正在尝试以不可能的方式操纵样式。
// this will work
modal.style.display = "block";
// this won't
modal.display.style = "none";
最后,使用window.onclick可能很难真正达到模态。其子元素上的每个click事件都不会具有模式元素的目标。 您可能会想得太多。
此外,如果要使用带有“ close”类的div作为触发器,请确保它实际上是可单击的(通过设置其宽度和高度)。
以下是一些工作代码:
HTML
<button id="trigger" class="btn primary">Open modal</button>
<div id="theModal" class="cd-box modal bg" style="display: none">
<div class=card>
<div class="title">
<h3>Title</h3>
</div>
<div class="close"></div>
<div class="inside">
<p>This is some content that goes into the modal. The width adjusts according to the content.</p>
</div>
<div class="buttons">
<button id="exit" class="btn secondary">Exit</button>
<button class="btn primary">Continue with action</button>
</div>
</div>
</div>
JavaScript
var modal = document.getElementById("theModal");
var btn = document.getElementById("trigger");
var exit = document.getElementById("exit");
var span = document.getElementsByClassName("close")[0];
btn.onclick = function () {
modal.style.display = "block";
}
exit.onclick = function () {
modal.style.display = "none";
}
window.onclick = function (event) {
console.log(event)
if (event.target == modal) {
modal.style.display = "none";
}
}
答案 1 :(得分:1)
First thing, you've not defined btn and you're binding an action on it. Second,
div.onclick = function() {
modal.display.style = "none"; // this is wrong , should be : modal.style.display
}
// Get the button that opens the modal
var btn = document.getElementById("myBtn");
// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];
// When the user clicks on the button, open the modal
btn.onclick = function() {
modal.style.display = "block";
}
// When the user clicks on <span> (x), close the modal
span.onclick = function() {
modal.style.display = "none";
}
答案 2 :(得分:1)
我对您的代码进行了许多修改,现在可以在此JSFiddle中使用:
https://jsfiddle.net/9ajsnx21/13/
JavaScript
var modal = document.getElementById("theModal");
var btn = document.getElementById("trigger");
var span = document.getElementsByClassName("close")[0];
btn.onclick = function() {
modal.style.display = "block";
}
span.onclick = function() {
modal.style.display = "none";
}
window.onclick = function(event) {
if (event.target == modal) {
modal.display.style = "none";
}
}
HTML
<button id="trigger" class="btn primary">Open modal</button>
<div id="theModal" class="cd-box modal bg">
<div class="modal-content">
<div class="title">
<h3>Title</h3>
</div>
<span class="close">×</span>
<div class="inside">
<p>This is some content that goes into the modal. The width adjusts according to the content.</p>
</div>
<div class="buttons">
<button class="btn secondary">Exit</button>
<button class="btn primary">Continue with action</button>
</div>
</div>
</div>
CSS
body {font-family: Arial, Helvetica, sans-serif;}
/* The Modal (background) */
.modal {
display: none; /* Hidden by default */
position: fixed; /* Stay in place */
z-index: 1; /* Sit on top */
padding-top: 100px; /* Location of the box */
left: 0;
top: 0;
width: 100%; /* Full width */
height: 100%; /* Full height */
overflow: 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 */
.modal-content {
background-color: #fefefe;
margin: auto;
padding: 20px;
border: 1px solid #888;
width: 80%;
}
/* The Close Button */
.close {
color: #aaaaaa;
float: right;
font-size: 28px;
font-weight: bold;
}
.close:hover,
.close:focus {
color: #000;
text-decoration: none;
cursor: pointer;
}
您使用的javascript效果很好,我将“ div”更改为“ span”。
主要问题是您似乎没有包含任何样式。包括您提供的示例中的样式都可以使它起作用。
答案 3 :(得分:0)
谢谢大家-您的所有回答都很有帮助,而且很重要,但是最大的问题是我的JS没有关闭,所以没有一个被触发。