简体   繁体   English

无法获得模态工作

[英]Can't get modal to work

I've literally copied and pasted the modal html, css, and javascript fro w3schools into my html and I still can't get this simple modal to show up. 我已经从w3schools复制了模态html,css和javascript并将其粘贴到我的html中,但仍然无法显示此简单模态。

Here is the relevant html. 这是相关的html。

 <button id="myBtn">Open Modal</button>

     <!-- The Modal -->
     <div id="myModal" class="modal">
         <!-- Modal content -->
         <div class="modal-content">
             <span class="close">&times;</span>
             <p>Some text in the Modal..</p>
         </div>
     </div>

Here is the css. 这是CSS。

.modal {
    display: none; /* Hidden by default */
    position: fixed; /* Stay in place */
    z-index: 1000; /* Sit on top */
    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/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;
}

And the javascript... 还有javascript ...

// Get the modal
var modal = document.getElementById('myModal');

// 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';
};

// When the user clicks anywhere outside of the modal, close it
window.onclick = function (event) {
    if (event.target === modal) {
        modal.style.display = 'none';
    }
};

This is all copied from w3schools how to section here . 这些都是从w3schools复制的,如何在这里进行介绍

I am using Materialize on the site as well. 我也在网站上使用Materialize。 And they DO have some css classes with the same name, modal, but I'm loading the above styles after I load the material css, and loading the javascript after I load the material javascript. 并且它们确实有一些具有相同名称,模态的css类,但是我在加载材料css之后加载了上面的样式,并且在加载材料javascript之后加载了javascript。

What happens is when the button is clicked, it will change colors, but the span and the p tag with the text never show. 发生的是单击该按钮时,它将更改颜色,但是span和带有文本的p标签从不显示。 When I click off of the button, the color goes back to the default. 当我单击按钮之外时,颜色恢复为默认值。 So it's like it's working but it's not showing the modal. 因此,就像正在运行,但未显示模式。

I've been working on this for way too long, hence why I've finally just copied and pasted the w3schools code. 我从事此工作的时间已经太久了,因此为什么我最后才复制并粘贴了w3schools代码。 But. 但。 it's still not working. 它仍然无法正常工作。 Can someone please fix this before I bang my head on my desk? 有人可以在我将头撞到桌子上之前解决此问题吗?

Prefix your custom class with something, so that they don't conflict with materializecss . 给自定义类添加前缀,以免与materializecss冲突。 Here I used _ as a prefix to remove the conflict. 在这里,我使用_作为前缀来消除冲突。

 // Get the modal var modal = document.getElementById('_myModal'); // 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'; }; // When the user clicks anywhere outside of the modal, close it window.onclick = function (event) { if (event.target === modal) { modal.style.display = 'none'; } }; 
 ._modal { display: none; /* Hidden by default */ position: fixed; /* Stay in place */ z-index: 1000; /* Sit on top */ 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/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; } 
 <link href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0-beta/css/materialize.min.css" rel="stylesheet"/> <div class="container"> <div class="section"> <button id="myBtn" class="btn-large waves-effect waves-light orange">Open Modal</button> <!-- The Modal --> <div id="_myModal" class="_modal"> <!-- Modal content --> <div class="_modal-content"> <span class="_close">&times;</span> <p>Some text in the Modal..</p> </div> </div> </div> </div> <script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0-beta/js/materialize.min.js"></script> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM