代码之家  ›  专栏  ›  技术社区  ›  MMelvin0581

无法让莫代尔工作

  •  0
  • MMelvin0581  · 技术社区  · 8 年前

    我已经从字面上复制并粘贴了w3school的模式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';
        }
    };
    

    这都是从W3学校“如何”部分复制的。 here .

    我也在使用网站上的物化。它们确实有一些具有相同名称、模式的CSS类,但是我在加载材质CSS之后加载上面的样式,在加载材质JavaScript之后加载javascript。

    当单击按钮时,它将更改颜色,但带文本的跨度和p标记永远不会显示。当我单击该按钮时,颜色将返回到默认值。所以它就像是在工作,但没有显示出模式。

    我在这方面工作的时间太长了,所以为什么我最终只是复制并粘贴了W3Schools代码。但是。它仍然不起作用。有人能在我把头撞到桌子上之前把这个修好吗?

    1 回复  |  直到 8 年前
        1
  •  0
  •   Ritwick Dey    8 年前

    为自定义类添加前缀,这样它们就不会与 materializecss . 这里我用过 _ 作为消除冲突的前缀。

    // 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>