RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 876975
Accepted
Евгений
Евгений
Asked:2020-09-04 03:32:48 +0000 UTC2020-09-04 03:32:48 +0000 UTC 2020-09-04 03:32:48 +0000 UTC

多个 js 模态的事件处理

  • 772

有几个模态窗口:

我徒劳的尝试的例子

打开模态窗口时,有一个按钮,单击时,窗口应该关闭,问题是该按钮仅在最后一个窗口上有效 。

javascript
  • 1 1 个回答
  • 10 Views

1 个回答

  • Voted
  1. Best Answer
    haswell
    2020-09-04T17:20:54Z2020-09-04T17:20:54Z

    您的页面不应包含具有相同 ID 的元素。你的代码没有解析,我画了自己的版本,你可以这样。

    document.addEventListener('DOMContentLoaded', function() {
    
      // Вешаем событие при нажатии на Show
      var Show = document.getElementsByClassName('button-light');
      [].forEach.call(Show, function(element, i) {       
        element.addEventListener('click', function(){
          showMessageDialog(i);
        });
      });
    
      // Вешаем событие при нажатии на Ok
      var Close = document.getElementsByClassName('button-primary');
      [].forEach.call(Close, function(element, i) {       
        element.addEventListener('click', function(){
          closeMessageDialog(i);
        });
      });
    
    });
    
    function showMessageDialog(i){
      var modal = document.getElementsByClassName('ui-message-box__wrapper')[i];   
      modal.style.display = "block";   
    }
    
    function closeMessageDialog(i){
      var modal = document.getElementsByClassName('ui-message-box__wrapper')[i];   
      modal.style.display = "none";   
    }
    .ui-message-box__wrapper {
    	display: none;
    	position: fixed;
    	top: 0;
    	bottom: 0;
    	left: 0;
    	right: 0;
    	z-index: 2002;
    	background-color: rgba(39,39,39,0.48);
    }
    
    .ui-message-box {
    	display: block;
    	position: fixed;
    	top: 50%;
    	left: 50%;
    	min-width: 400px;
    	transform: translateX(-50%);
    	padding: 12px 14px;
    	background-color: #edf2fc;
    	border: 1px solid #ebeef5;
    	border-radius: 8px;
    	z-index: 2000;
    	animation: fadeInDown 0.3s;
    	overflow: hidden;
    }
    
    .ui-message-box .button-close {
    	width: 22px;
    	height: 22px;
    }
    
    .ui-message-box__header {
    	display: flex;
    	align-items: center;
    	justify-content: space-between;
    	padding-bottom: 8px;
    	margin-bottom: 8px;
    }
    
    .ui-message-box__content {
    	margin-bottom: 16px;
    }
    
    .ui-message-box__footer {
    	text-align: right;
      cursor: pointer;
    }
    
    .message-box-title {
    	font-size: rem(24);
    }
    
    @-moz-keyframes fadeInDown {
    	from {
    		opacity: 0;
    		-webkit-transform: translate3d(0, -100%, 0);
    		transform: translate3d(0, -100%, 0);
    	}
    
    	to {
    		opacity: 1;
    		-webkit-transform: translate3d(0, 0, 0);
    		transform: translate3d(0, 0, 0);
    	}
    }
    
    @-webkit-keyframes fadeInDown {
    	from {
    		opacity: 0;
    		-webkit-transform: translate3d(0, -100%, 0);
    		transform: translate3d(0, -100%, 0);
    	}
    
    	to {
    		opacity: 1;
    		-webkit-transform: translate3d(0, 0, 0);
    		transform: translate3d(0, 0, 0);
    	}
    }
    
    @-o-keyframes fadeInDown {
    	from {
    		opacity: 0;
    		-webkit-transform: translate3d(0, -100%, 0);
    		transform: translate3d(0, -100%, 0);
    	}
    
    	to {
    		opacity: 1;
    		-webkit-transform: translate3d(0, 0, 0);
    		transform: translate3d(0, 0, 0);
    	}
    }
    
    @keyframes fadeInDown {
    	from {
    		opacity: 0;
    		-webkit-transform: translate3d(0, -100%, 0);
    		transform: translate3d(0, -100%, 0);
    	}
    
    	to {
    		opacity: 1;
    		-webkit-transform: translate3d(0, 0, 0);
    		transform: translate3d(0, 0, 0);
    	}
    }
    <section class="message-box-doc">
        <div class="container"><span class="ui-title-2">Standart Message-box:</span>
          <div class="example-empty">
            <div class="ui-message-box__wrapper" id="exampleModal">
              <div class="ui-message-box">
                <div class="ui-message-box__header"><span class="message-box-title">Title</span><span class="button-close" ></span></div>
                <div class="ui-message-box__content"><span>Message is here</span></div>
                <div class="ui-message-box__footer">
                  <div class="button button-primary">OK</div>
                </div>
              </div>
            </div>
            <div class="button button-light">Show</div>
          </div>
        </div>
      </section>
      <section class="message-box-doc">
        <div class="container"><span class="ui-title-2">Standart Message-box:</span>
          <div class="example-empty">
            <div class="ui-message-box__wrapper" id="exampleModal2">
              <div class="ui-message-box">
                <div class="ui-message-box__header"><span class="message-box-title">Title2</span><span class="button-close" ></span></div>
                <div class="ui-message-box__content"><span>Message is here2</span></div>
                <div class="ui-message-box__footer">
                  <div class="button button-primary">OK</div>
                </div>
              </div>
            </div>
            <div class="button button-light">Show</div>
          </div>
        </div>
      </section>

    • 2

相关问题

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    是否可以在 C++ 中继承类 <---> 结构?

    • 2 个回答
  • Marko Smith

    这种神经网络架构适合文本分类吗?

    • 1 个回答
  • Marko Smith

    为什么分配的工作方式不同?

    • 3 个回答
  • Marko Smith

    控制台中的光标坐标

    • 1 个回答
  • Marko Smith

    如何在 C++ 中删除类的实例?

    • 4 个回答
  • Marko Smith

    点是否属于线段的问题

    • 2 个回答
  • Marko Smith

    json结构错误

    • 1 个回答
  • Marko Smith

    ServiceWorker 中的“获取”事件

    • 1 个回答
  • Marko Smith

    c ++控制台应用程序exe文件[重复]

    • 1 个回答
  • Marko Smith

    按多列从sql表中选择

    • 1 个回答
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Suvitruf - Andrei Apanasik 什么是空? 2020-08-21 01:48:09 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5