我正在尝试向我的html页面添加模式,但是关闭按钮似乎有问题

时间:2020-06-02 14:31:27

标签: javascript html css

我在网页中创建了一个模态,它显示了何时单击,但是单击关闭时没有任何反应(没有关闭)的想法吗?

function voice() {
  $("#myModal").modal('show');
  $('.try').css("opacity", "0.3");

}

function closeModal() {
  $("#myModal").modal('hide');
  $('.try').css("opacity", "1");
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
<a class="boxed-btn3 " onclick="voice()">----- Try -----</a>
<div id="myModal" class="modal fade" tabindex="-1" style="opacity:1;">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <h3 class="modal-title">Try saying something...</h3>
        <button type="button" class="close" data-dismiss="modal" onclick="closeModal()">&times;</button>

      </div>
      <div class="modal-body">
        <i class="fas fa-microphone"></i>
        <button class="button button5"></button>
      </div>

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

2 个答案:

答案 0 :(得分:1)

我通过更改此方法解决了

<div id="myModal" class="modal fade" tabindex="-1" style="opacity:1;">

对此

<div id="myModal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" data-backdrop="false">

答案 1 :(得分:0)

您的HTML必须具有正确的顺序才能在您的PC上运行!

<!doctype html>
<html lang="en">

<head>
  <!-- Required meta tags -->
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

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

  <title>Hello, world!</title>
</head>

<body>

  <a class="boxed-btn3 " onclick="voice()">----- Try -----</a>
  <div id="myModal" class="modal fade" tabindex="-1" style="opacity:1;">
    <div class="modal-dialog">
      <div class="modal-content">
        <div class="modal-header">
          <h3 class="modal-title">Try saying something...</h3>
          <button type="button" class="close" data-dismiss="modal" onclick="closeModal()">&times;</button>

        </div>
        <div class="modal-body">
          <i class="fas fa-microphone"></i>
          <button class="button button5"></button>
        </div>

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

  <script>
    function voice() {
      $("#myModal").modal('show');
      $('.try').css("opacity", "0.3");

    }

    function closeModal() {
      $("#myModal").modal('hide');
      $('.try').css("opacity", "1");
    }
  </script>

  <!-- Optional JavaScript -->
  <!-- jQuery first, then Popper.js, then Bootstrap JS -->
  <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous">
  </script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous">
  </script>
  <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous">
  </script>
</body>

</html>