多个按钮仅在代码中打开最新的模式



这里编码很新手,但我正在编写一个 Web 应用程序,我想在页面顶部有两个按钮。 一个用于登录/创建帐户,另一个用于添加对页面的评论。我添加了第一个按钮,并在按下时让它显示模态,这一切都工作正常。然后,我尝试添加第二个按钮来显示另一个模态,但现在两个按钮都只显示第二个模态。

每个模态在右上角都有一个小的"x"来退出,这现在也不起作用。这是我的代码

风格都在头脑中。

    /* The Modal (background) */
    .modal {
        display: none;
        position: fixed;
        z-index: 1;
        padding-top: 100px;
        left: 0;
        top: 0;
        width: 100%;
        height: 100%;
        overflow: auto;
        background-color: rgb(0,0,0);
        background-color: rgba(0,0,0,0.3); /* Used to grey out the rest of the screen */
    }
    /* Modal Content */
    .modal-content {
        background-color: #fefefe; /* Background colour of Modal */
        margin: auto;
        padding: 20px;
        border: 1px solid #888;
        width: 50%;
    }
    /* 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;
    }
</style>

其余代码都在正文中。

<h2>Modal Testing</h2>
<!-- Buttons to open Modals -->
<button id="loginBtn">Account</button>
<button id="reviewBtn">Add Review</button>
<!-- The Modals -->
<!-- Login/Create Account Modal -->
<div id="loginModal" class="modal">
<!-- Modal content -->
<div class="modal-content">
    <span class="close">&times;</span>
    <p>Login details</p>
    <!-- Allows the user to input information-->
    <form action="/action_page.php">
        Username:<br>
        <input type="text" name="username" value="">
        <br>
        Password:<br>
        <input type="password" value="" id="userInput">
        <br><br>
        <input type="checkbox" onclick="visible()">Show Password
    </form>
    <button id="signInBtn">Sign In</button>
    <p>Not got an account? Sign up now!</p>
    <button id="signUpBtn">Sign Up</button>
    <button id="cancelBtn">Cancel</button>
</div>
</div>
<script>
function visible(){
    var x = document.getElementById("userInput");
    if (x.type === "password") {
        x.type = "text";
    } else {
        x.type = "password";
    }
}
</script>
<script>
// Get the modal
var modal = document.getElementById('loginModal');
// Get the button that opens the modal
var btn = document.getElementById("loginBtn");
// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];
// When the user clicks 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";
    }
}
</script>
<!-- Add Review Modal -->
<div id="addReviewModal" class="modal">
<!-- Modal content -->
<div class="modal-content">
    <span class="close">&times;</span>
    <p>Add Review</p>
    <!-- Allows the user to input information-->
    <form action="/review_page.php">
        Location:<br>
        <input type="text" name="location" value="">
        <br>
        Comments:<br>
        <input type="text" name="comments" value="">
        <br>
    </form>
    <button id="submitBtn">Submit Review</button>
    <button id="cancelBtn">Cancel</button>
</div>
</div>
<script>
// Get the modal
var modal = document.getElementById('addReviewModal');
// Get the button that opens the modal
var btn = document.getElementById("reviewBtn");
// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];
// When the user clicks 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";
    }
}
</script>

你在不同的<script>标签中使用你的 JavaScript。这并不意味着它们是单独的块,它们只是相互附加。这意味着,当您在一个块中声明变量modal然后在第二个块中再次声明它时,变量将被覆盖。我将 Var 名称更改为 XXX1 和 XXX2。

function visible(){
    var x = document.getElementById("userInput");
    if (x.type === "password") {
        x.type = "text";
    } else {
        x.type = "password";
    }
}
// Get the modal
var modal1 = document.getElementById('loginModal');
// Get the button that opens the modal
var btn1 = document.getElementById("loginBtn");
// Get the <span> element that closes the modal
var span1 = document.getElementsByClassName("close")[0];
// When the user clicks the button, open the modal
btn1.onclick = function() {
    modal1.style.display = "block";
}
// When the user clicks on <span> (x), close the modal
span1.onclick = function() {
    modal1.style.display = "none";
}
// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
    if (event.target == modal1) {
        modal1.style.display = "none";
    }
}
// Get the modal
var modal2 = document.getElementById('addReviewModal');
// Get the button that opens the modal
var btn2 = document.getElementById("reviewBtn");
// Get the <span> element that closes the modal
var span2 = document.getElementsByClassName("close")[0];
// When the user clicks the button, open the modal
btn2.onclick = function() {
    modal2.style.display = "block";
}
// When the user clicks on <span> (x), close the modal
span2.onclick = function() {
    modal2.style.display = "none";
}
// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
    if (event.target == modal2) {
        modal2.style.display = "none";
    }
}
    .modal {
        display: none;
        position: fixed;
        z-index: 1;
        padding-top: 100px;
        left: 0;
        top: 0;
        width: 100%;
        height: 100%;
        overflow: auto;
        background-color: rgb(0,0,0);
        background-color: rgba(0,0,0,0.3); /* Used to grey out the rest of the screen */
    }
    /* Modal Content */
    .modal-content {
        background-color: #fefefe; /* Background colour of Modal */
        margin: auto;
        padding: 20px;
        border: 1px solid #888;
        width: 50%;
    }
    /* 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;
    }
<h2>Modal Testing</h2>
<!-- Buttons to open Modals -->
<button id="loginBtn">Account</button>
<button id="reviewBtn">Add Review</button>
<!-- The Modals -->
<!-- Login/Create Account Modal -->
<div id="loginModal" class="modal">
<!-- Modal content -->
<div class="modal-content">
    <span class="close">&times;</span>
    <p>Login details</p>
    <!-- Allows the user to input information-->
    <form action="/action_page.php">
        Username:<br>
        <input type="text" name="username" value="">
        <br>
        Password:<br>
        <input type="password" value="" id="userInput">
        <br><br>
        <input type="checkbox" onclick="visible()">Show Password
    </form>
    <button id="signInBtn">Sign In</button>
    <p>Not got an account? Sign up now!</p>
    <button id="signUpBtn">Sign Up</button>
    <button id="cancelBtn">Cancel</button>
</div>
</div>
<!-- Add Review Modal -->
<div id="addReviewModal" class="modal">
<!-- Modal content -->
<div class="modal-content">
    <span class="close">&times;</span>
    <p>Add Review</p>
    <!-- Allows the user to input information-->
    <form action="/review_page.php">
        Location:<br>
        <input type="text" name="location" value="">
        <br>
        Comments:<br>
        <input type="text" name="comments" value="">
        <br>
    </form>
    <button id="submitBtn">Submit Review</button>
    <button id="cancelBtn">Cancel</button>
</div>
</div>

现在按钮打开相应的模态。

如果我认为您是"关闭 X"的事情,我会发布更新。

对不起,我的英语;)不好

而不是

document.getElementsByClassName("close")[0];

modal.getElementsByClassName("close")[0];

最新更新