Javascript模式无法打开

时间:2018-04-23 09:43:18

标签: javascript modal-dialog

所以我有这个网站,我正在尝试使用模态作为我的登录并注册 - 但我无法让它们工作。

它与我的z索引有什么关系,或者为什么它们没有触发?当我点击我的链接时,绝对没有任何反应 - 是因为它们不是按钮吗?

我已经包含了我的HTML,CSS和JavaScript。

HTML

<header>
    <div class="container-header">
        <div class="logo">
            <h1><i class="fab fa-leanpub"></i><span class="highlight">AAU</span>Tutor</h1>
        </div>
        <nav>
            <ul>
                <li><a href="">Home</a></li>
                <li><a href="">About</a></li>
                <li><a href="">Contact</a></li>
                <li><a class="button" data-modal="modalLogin">Login</a></li>
                <li><a class="button" data-modal="modalSignUp">Sign Up</a></li>
            </ul>
        </nav>
    </div>
</header>

<section class="modal-windows">
    <div id="modalLogin" class="modal">
        <div class="modal-content">
            <div class="modal-header">
                <h3>Log in</h3>
                <a class="close">&times;</a>
            </div>
            <div class="modal-body">

            </div>
            <div class="modal-footer">
                <a class="message">Sign Up here!</a>
            </div>
        </div>
    </div>
    <div id="modalSignUp" class="modal">
        <div class="modal-content">
            <div class="modal-header">
                <h3>Log in</h3>
                <a class="close">&times;</a>
            </div>
            <div class="modal-body">

            </div>
            <div class="modal-footer">
                <a class="message">Log in here!</a>
            </div>
        </div>
    </div>
</section>

<section class="home-hero">
    <div class="container">
        <h1 class="title">In need of a tutor?
            <span>Find a tutor at AAU for your needs</span>
        </h1>
        <a href="" class="button button-accent">Sign up now!</a>
    </div>
</section>

<div class="container">
    <section class="home-about">
        <div class="home-about-textbox">
            <h1>Who we are</h1>
            <p>AAU Tutor is the best way to find a tutor</p>
            <p>AAU Tutor is a community where members can find a tutor, or tutor other students at AAU</p>
        </div>
    </section>
</div>

<section class="services">
    <div class="container">
        <div class="row">
            <div class="col-4">
                <i class="far fa-calendar-alt fa-5x"></i>
                <h3>See availibility</h3>
                <p>Have a look at the calendar</p>
            </div>
            <div class="col-4">
                <i class="fas fa-tasks fa-5x"></i>
                <h3>Read reviews</h3>
                <p>Read reviews of the tutors</p>
            </div>
            <div class="col-4">
                <i class="fas fa-comments fa-5x"></i>
                <h3>Contact tutors</h3>
                <p>Contact tutors for free</p>
            </div>
        </div>
    </div>
</section>

<footer>
    <div class="container">
        <p>AAU Tutor, Copyright &copy; 2018</p>
    </div>
</footer>


<script src="js/main.js"></script>
<script src="js/modal.js"></script>

</body>

CSS

.modal {
  display: none;
  position: fixed;
  z-index: 8888;
  left: 0;
  top: 0;
  width: 100%;
  height: 100vh;
  overflow: auto;
  background-color: rgb(0, 0, 0);
  background-color: rgba()0, 0, 0, .4;
}

.modal-content {
  background-color: #fefefe;
  margin: 10vh auto;
  padding: 2em;
  border: .25em solid #888;
  width: 90%;
}

@media (min-width:60rem) {
  .modal-content {
      width: 30%;
  }
}

.close {
  color: #aaa;
  float: right;
  font-size: 2em;
  font-weight: bold;
}

.close:hover,
.close:focus {
  color: black;
  text-decoration: none;
  cursor: pointer;
}

这是我的JavaScript

var modalBtns = [...document.querySelectorAll(".button")];
modalBtns.forEach(function (btn) {
    btn.onclick = function () {
        var modal = btn.getAttribute('data-modal');
        document.getElementById(modal).style.display = "block;"
    }
});

var closeBtns = [...document.querySelectorAll(".close")];
closeBtns.forEach(function (btn) {
    btn.onclick = function () {
        var modal = btn.closest('.modal');
        modal.style.display = "none";
    }
});

window.onclick = function (event) {
    if (event.target.className === "modal") {
        event.target.style.display = "none";
    }
}

2 个答案:

答案 0 :(得分:0)

你刚才放错了结束语。请更新您的代码,如下所示:

var modalBtns = [...document.querySelectorAll(".button")];
modalBtns.forEach(function(btn){
    btn.onclick = function(e) {
    e.preventDefault();
       var modal = btn.getAttribute('data-modal');
       document.getElementById(modal).style.display = "block";
    }
});

var closeBtns = [...document.querySelectorAll(".close")];
closeBtns.forEach(function(btn){
  btn.onclick = function() {
    var modal = btn.closest('.modal');
    modal.style.display = "none";
 }
});

window.onclick = function(event) {
  if (event.target.className === "modal") {
   event.target.style.display = "none";
 }
}

答案 1 :(得分:0)

正如@Kaddath所说:

document.getElementById(modal).style.display =“block;”应该是“阻止”而不用分号,这是你的错误.. - Kaddath

非常感谢!