无法让模态工作

时间:2018-05-27 03:26:49

标签: javascript html css user-interface materialize

我已经将w3schools中的模态html,css和javascript复制并粘贴到我的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>

这是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;
}

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

这些都是从w3schools复制到如何here部分。

我也在网站上使用Materialise。并且他们有一些同名的css类,modal,但是我在加载材质css之后加载上面的样式,并在加载材质javascript后加载javascript。

当点击按钮时会发生什么变化,它会改变颜色,但是带有文本的span和p标签永远不会显示。当我单击按钮时,颜色将恢复为默认值。所以它就像是在工作,但它没有显示模态。

我一直在研究这个问题太久了,因此我最终只是复制并粘贴了w3schools代码。但。它还没有用。在我把头撞到桌子上之前,有人可以解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

使用某些内容为您的自定义类添加前缀,以免它们与materializecss发生冲突。在这里,我使用_作为前缀来消除冲突。

&#13;
&#13;
// 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';
    }
};
&#13;
._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;
}
&#13;
<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>
&#13;
&#13;
&#13;