模态关闭按钮不起作用 [英] Modal close button not working

查看:70
本文介绍了模态关闭按钮不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是代码.如果我打开模态,关闭按钮将不起作用,直到页面刷新后才会关闭.

//获取模态var modal = document.getElementById('myModal');//获取打开模态的按钮var btn = document.getElementById("myBtn");//获取 <span>关闭模态的元素var span = document.getElementsByClassName("close")[0];//当用户点击按钮时,打开模态btn.onclick = 函数(){modal.style.display = "块";}//当用户点击<span>(x)、关闭模态span.onclick = 函数(){modal.style.display = "无";}//当用户点击模态之外的任何地方时,关闭它window.onclick = 函数(事件){if (event.target == modal) {modal.style.display = "无";}}

/* 创建三列等宽 */.列 {向左飘浮;宽度:33.3%;填充:8px;}/* 样式列表 */.价格 {列表样式类型:无;边框:1px 实心 #eee;边距:0;填充:0;-webkit-transition: 0.3s;过渡:0.3s;}/* 在悬停时添加阴影 */.价格:悬停{box-shadow: 0 8px 12px 0 rgba(0, 0, 0, 0.2)}/* 定价标题 */.price .header {背景颜色:#111;白颜色;字体大小:25px;}/* 列出项目 */.price li {边框底部:1px 实心#eee;填充:20px;文本对齐:居中;}/* 灰名单项 */.price .grey {背景色:#eee;字体大小:20px;}/* 注册"按钮 */.按钮 {背景色:#4CAF50;边界:无;白颜色;填充:10px 25px;文本对齐:居中;文字装饰:无;字体大小:18px;}/* 将三列的宽度更改为 100%(在小屏幕上水平堆叠)*/@media only screen and (max-width: 600px) {.列 {宽度:100%;}}/* 模态(背景) */.modal {显示:无;/* 默认隐藏 */位置:固定;/* 原地不动 */z-索引:1;/* 坐在上面 */左:0;顶部:0;宽度:100%;/* 全屏宽度 */高度:100%;/* 全高 */溢出:自动;/* 根据需要启用滚动 */背景颜色:rgb(0, 0, 0);/* 后备颜色 */背景颜色:rgba(0, 0, 0, 0.4);/* 黑色不透明 */}/* 模态内容/框 */.modal-content {背景颜色:#fefefe;保证金:15% 自动;/* 15% 从顶部居中 */填充:20px;边框:1px 实心 #888;宽度:80%;/* 可能更多或更少,取决于屏幕大小 */}/* 关闭按钮 */.关闭 {颜色:#aaa;浮动:对;字体大小:28px;字体粗细:粗体;}.close:悬停,.close:focus {颜色:黑色;文字装饰:无;光标:指针;}

<ul class="价格"><li class="header">硬盘格式</li><li class="grey">£10</li><li>格式化硬盘</li><li>从驱动器中删除所有文件</li><li>全新安装 Windows</li><li class="grey"><button onclick="document.getElementById('myModal').style.display='block'">Sign向上</按钮></li>

<!-- 模态--><div id="myModal" class="modal"><!-- 模态内容--><div class="modal-content"><span class="close">&times;</span><p>Modal 中的一些文本..</p>

我想要的只是在单击按钮时关闭模态.

解决方案

在你的代码中没有 id myBtn

的元素

所以删除以下代码

//获取打开模态的按钮var btn = document.getElementById("myBtn");//当用户点击按钮时,打开模态btn.onclick = 函数(){modal.style.display = "块";}

它会正常工作

This is the code. If I open the modal, the close button will not work and will not close until the page is refreshed.

// Get the modal
var modal = document.getElementById('myModal');

// Get the button that opens the modal
var btn = document.getElementById("myBtn");

// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];

// When the user clicks the button, open the modal 
btn.onclick = function() {
  modal.style.display = "block";
}

// When the user clicks on <span> (x), close the modal
span.onclick = function() {
  modal.style.display = "none";
}

// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
  if (event.target == modal) {
    modal.style.display = "none";
  }
}

/* Create three columns of equal width */

.columns {
  float: left;
  width: 33.3%;
  padding: 8px;
}


/* Style the list */

.price {
  list-style-type: none;
  border: 1px solid #eee;
  margin: 0;
  padding: 0;
  -webkit-transition: 0.3s;
  transition: 0.3s;
}


/* Add shadows on hover */

.price:hover {
  box-shadow: 0 8px 12px 0 rgba(0, 0, 0, 0.2)
}


/* Pricing header */

.price .header {
  background-color: #111;
  color: white;
  font-size: 25px;
}


/* List items */

.price li {
  border-bottom: 1px solid #eee;
  padding: 20px;
  text-align: center;
}


/* Grey list item */

.price .grey {
  background-color: #eee;
  font-size: 20px;
}


/* The "Sign Up" button */

.button {
  background-color: #4CAF50;
  border: none;
  color: white;
  padding: 10px 25px;
  text-align: center;
  text-decoration: none;
  font-size: 18px;
}


/* Change the width of the three columns to 100% 
    (to stack horizontally on small screens) */

@media only screen and (max-width: 600px) {
  .columns {
    width: 100%;
  }
}


/* The Modal (background) */

.modal {
  display: none;
  /* Hidden by default */
  position: fixed;
  /* Stay in place */
  z-index: 1;
  /* Sit on top */
  left: 0;
  top: 0;
  width: 100%;
  /* Full width */
  height: 100%;
  /* Full height */
  overflow: auto;
  /* Enable scroll if needed */
  background-color: rgb(0, 0, 0);
  /* Fallback color */
  background-color: rgba(0, 0, 0, 0.4);
  /* Black w/ opacity */
}


/* Modal Content/Box */

.modal-content {
  background-color: #fefefe;
  margin: 15% auto;
  /* 15% from the top and centered */
  padding: 20px;
  border: 1px solid #888;
  width: 80%;
  /* Could be more or less, depending on screen size */
}


/* The Close Button */

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

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

<div class="columns">
  <ul class="price">
    <li class="header">Hard Drive Format</li>
    <li class="grey">£10</li>
    <li>Format Hard Drive</li>
    <li>Removes ALL Files From Drive</li>
    <li>Fresh Install Windows</li>
    <li class="grey"><button onclick="document.getElementById('myModal').style.display='block'">Sign 
    Up</button></li>
  </ul>
</div>

<!-- The Modal -->
<div id="myModal" class="modal">

  <!-- Modal content -->
  <div class="modal-content">
    <span class="close">&times;</span>
    <p>Some text in the Modal..</p>
  </div>

</div>

All I want is for the modal to close when the button is clicked.

解决方案

In your code there is no element with id myBtn

so remove the following code

// Get the button that opens the modal
var btn = document.getElementById("myBtn");

// When the user clicks the button, open the modal 
btn.onclick = function() {
modal.style.display = "block";
}

and it will work fine

这篇关于模态关闭按钮不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
前端开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆