javascript - 模态框不显示

标签 javascript html css modal-dialog

我正在尝试在网站上制作模态框,但它没有显示。模态框中的内容很好,但如果可能需要,我还附上了内容的 CSS 代码。

document.getElementById('buttonL').addEventListener("click", function() {
document.querySelector('.bg-modal').style.display = "flex";
});

document.querySelector('.close').addEventListener("click", function() {
document.querySelector('.bg-modal').style.display = "none";
});
.bg-modal{
background-color: rgba(0, 0, 0, 0.5);
width: 100%;
height: 100%;
position: fixed;
display: none;
justify-content: center;
align-items: center;
bottom: 0px;
}

.modal-content{
width: 600px;
height: 300px;
background-color: white;
opacity: 1;
border-radius: 2px;
}

.modal-content h1{
align-items: center;
text-align: center;
margin-top: 40px;
font-family: 'Rubik', sans-serif;
}

.modal-content img{
float: left;
width: 160px;
margin-top: 20px;
margin-left: 60px;
}

.modal-content p{
float: right;
margin-top: 20px;
margin-right: 60px;
font-size: 16px;
}


.close {
position: relative;
float: right;
margin-right: 8px;
font-size: 32px;
color: #333;
background-color: none;
cursor: pointer;
}
<button id="buttonL" class="buttonLegal">LEGAL DETAILS</button>
<div class="bg-modal">
    <div class="modal-content">
        <div class="close">✖</div>
        <h1>LEGAL DETAILS</h1>
        <img src="someimagehere.jpg">
        <p>some text here</p>
    </div>
</div>

我搜索了整个互联网并尝试了很多方法,但仍然无法显示模态框。我想我的 Javascript 代码可能有错误,但我不确定。所以,我希望你能在这件事上帮助我。谢谢!

最佳答案

您可以尝试来自 https://www.w3schools.com/ 的这个简单模态代码

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
</head>
<body>

<div class="container">
  <h2>Modal Example</h2>
  <!-- Trigger the modal with a button -->
  <button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal">Open Modal</button>

  <!-- Modal -->
  <div class="modal fade" id="myModal" role="dialog">
    <div class="modal-dialog">

      <!-- Modal content-->
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal">&times;</button>
          <h4 class="modal-title">Modal Header</h4>
        </div>
        <div class="modal-body">
          <p>Some text in the modal.</p>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        </div>
      </div>

    </div>
  </div>

</div>

</body>
</html>

关于javascript - 模态框不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59134558/

相关文章:

Javascript 调用一个函数,并且在它返回之前不会再次调用它

html - 无法居中有序列表标题

javascript - 如何在点击 div 时添加弹跳效果

javascript - 为什么当我点击打印按钮时@media print 的选择器没有显示正确的格式?

javascript - Chrome 不适用于线性渐变的属性偏移动画

javascript - 使用 canvas + JS 将对象从一个 (x,y) 移动到另一个 (x,y)

javascript - JS/TS 在不删除分隔符的情况下通过分隔符拆分字符串

javascript - 根据区域划分谷歌地图

html - CSS :not is not working

javascript - jQuery 输入表单问题