无法切换或关闭模式

时间:2019-02-27 15:52:36

标签: html modal-dialog toggle

我试图像在互联网上提供的示例中那样打开和关闭一个模式,但似乎我无法切换(它始终处于打开状态)并且我什至无法关闭它:

模式

<!DOCTYPE html>
<html>

<head>

</head>

<body>
    <div class="container">

        <button class="btn btn-primary" data-target="#campModal" data-toggle="modal"></button>

        <div class="modal" id="campModal" tabindex="-1">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <button class="close" data-dismiss="modal">&times;</button>
                        <h4 class="modal-title">Create</h4>
                    </div>
                    <div class="modal-body">
                        <form>
                            <div class="form-group">
                                <input type="text" name="Name" id="name"/>

                            </div>
                            <div class="form-group">
                                <input type="text" name="Password" id="password"/>
                            </div>
                        </form>
                    </div>
                    <div class="modal-footer">
                        <button class="btn btn-primary">Submit</button>
                        <button class="btn btn-primary" data-dismiss="modal">Close</button>
                    </div>

                </div>
            </div>
        </div>
    </div>
</body>

</html>

1 个答案:

答案 0 :(得分:1)

一旦包含必需的依赖项,您的代码就会起作用。在Google Chrome开发人员工具(或Firefox)上查看控制台输出通常会有助于查找缺少的依赖项。

.btn {
  position: absolute;
  /* Center the button */
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}
<!-- JQuery CDN -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<!-- Popper.js CDN, required by Bootstrap -->
<script src="https://unpkg.com/popper.js"></script>

<!-- Bootstrap javascript CDN -->
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>

<!-- Bootstrap CSS CDN -->
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">

<body>

  <div class="container">

    <button class="btn btn-primary" data-target="#campModal" data-toggle="modal">OPEN MODAL</button>

    <div class="modal" id="campModal" tabindex="-1">
      <div class="modal-dialog">
        <div class="modal-content">
          <div class="modal-header">
            <button class="close" data-dismiss="modal">&times;</button>
            <h4 class="modal-title">Create</h4>
          </div>
          <div class="modal-body">
            <form>
              <div class="form-group">
                <input type="text" name="Name" id="name" />

              </div>
              <div class="form-group">
                <input type="text" name="Password" id="password" />
              </div>
            </form>
          </div>
          <div class="modal-footer">
            <button class="btn btn-primary">Submit</button>
            <button class="btn btn-primary" data-dismiss="modal">Close</button>
          </div>

        </div>
      </div>
    </div>
  </div>
</body>

</html>

我用相同的代码创建了一个Codepen