开发者问题收集

点击后模态框不显示

2020-01-23
40

我创建了一个模态框,但它无法显示,它在我用 React 创建的应用程序上运行良好,但使用原始 javascript(在另一个项目上)则无法运行。

html、css 和 javascript 代码的链接: https://codepen.io/J-Kazama/pen/WNbPoZB

如果我将 .bg-modal 的显示从 none 更改为 flex ,模态框就会显示出来,所以我猜问题出在 javascript 上。

1个回答

从你的 JS 中删除导入,它就可以正常工作了...

(function() {

  document.getElementById('revAddButton').addEventListener('click', function() {
    document.querySelector('.bg-modal').style.display = 'flex'
  });
})()
.btn {
  display: inline-block;
  border: none;
  padding: 6px 12px;
  margin-bottom: 0;
  font-size: 14px;
  font-weight: normal;
  text-align: center;
  cursor: pointer;
  border-radius: 4px;
}

.btn-red {
  text-decoration: none;
  color: #fff;
  background: #ff4742;
  border: 2px solid #ff4742;
  border-radius: 25px;
  margin-right: 7px;
}

.btn-red:hover,
.btn-red:active {
  border: 2px solid #ff4742;
  text-decoration: none;
  color: #ff4742;
  background-color: white;
}

.div1:hover,
.div1:focus {
  color: #555555;
  background: rgba(60, 186, 84, 0.25);
}

.parent {
  width: 100%;
  background-color: #f8f9fb;
  margin-top: 160px;
  height: 500px;
  display: flex;
  flex-flow: row wrap;
  align-items: center;
  justify-content: center;
  align-content: flex-start;
}

.div1 {
  background-color: rgba(70, 220, 100, 0.55);
  position: relative;
  margin: 5px;
  width: 275px;
  height: 220px;
  border: 0;
  border-radius: 5px;
  font-family: 'Varela Round', sans-serif;
}

 ::-webkit-input-placeholder {
  font-family: 'Varela Round', sans-serif;
}

 ::-webkit-moz-input-placeholder {
  font-family: 'Varela Round', sans-serif;
}

input[type="text"] {
  font-family: 'Varela Round', sans-serif;
}

.text {
  font-family: 'Varela Round', sans-serif;
}

.closeBtn {
  color: #cccccc;
  float: right;
  font-size: 30px;
}

.closeBtn:hover,
.closeBtn:focus {
  color: #000000;
  text-decoration: none;
  cursor: pointer;
}

.bg-modal {
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.7);
  position: absolute;
  top: 0;
  justify-content: center;
  align-items: center;
  display: none;
  filter: blur(0px);
}

.modal-content {
  background-color: #f4f4f4;
  width: 600px;
  height: 600px;
  border-radius: 6px;
  text-align: center;
  padding: 20px;
  margin-top: 30px;
  position: relative;
}

.class-input {
  width: 50%;
  display: block;
  margin: 8px auto;
}

.form-control {
  display: inline-block;
  width: 450px;
  height: 34px;
  padding: 6px 12px;
  font-size: 20px;
  font-family: Nunito, serif;
  line-height: 1.42857143;
  color: #555;
  background-color: #fff;
  background-image: none;
  border: 1px solid #ccc;
  border-radius: 6px;
  -webkit-box-shadow: inset 0 1px 1px rgba(0, 0, 0, .075);
  box-shadow: inset 0 1px 1px rgba(0, 0, 0, .075);
  -webkit-transition: border-color ease-in-out .15s, -webkit-box-shadow ease-in-out .15s;
  -o-transition: border-color ease-in-out .15s, box-shadow ease-in-out .15s;
  transition: border-color ease-in-out .15s, box-shadow ease-in-out .15s;
}

.comp-input {
  text-align: left;
  background-color: #fff!important;
  border: 1px solid #e0e0e0!important;
  height: 50px!important;
  padding-left: 15px;
  padding-right: 15px;
  border-radius: 3px!important;
}

.input-lg {
  height: 46px;
  padding: 10px 16px;
  font-size: 18px;
  line-height: 1.3333333;
  border-radius: 6px;
}

.sub-button {
  margin-top: 10px;
}

.container {
  display: flex;
  justify-content: center;
  align-self: center;
  transition: 1s;
  padding: 20px;
  position: relative;
  background: #ff4742;
}

.container .content {
  position: relative;
  max-width: 800px;
}
<link href="index.css" rel="stylesheet" type="text/css">
<link href="https://fonts.googleapis.com/css?family=Varela+Round&display=swap" rel="stylesheet"></link>
<h1 class="text">Boutique</h1>
<a id="revAddButton" role="button" target="_blank" class="btn btn-red text">Add a Comment</a>
<div class="bg-modal" id="modal">
  <div class="modal-content">
    <span class="closeBtn" id="closeBtn">&times;</span>
    <form action="">
      <h2 class="text">Add your comment</h2>
      <input class="class-input form-control comp-input input-lg" type="text" placeholder="Name" />
      <a role="button" href="/main" target="_blank" class="btn btn-red text sub-button">Submit</a>
    </form>
  </div>
</div>
Akber Iqbal
2020-01-23