简体   繁体   English

关闭子模式,同时保持父级仅在关闭按钮单击时打开

[英]Close child modal while keeping parent open only on close button click

So I'm running into some issues and I can't figure out how to close a child modal and keep the parent modal open, so I wanted to ask the community to see if I can get some assistance.所以我遇到了一些问题,我不知道如何关闭子模式并保持父模式打开,所以我想问问社区,看看我是否可以获得一些帮助。

So here is what I'm attempting to do:所以这就是我正在尝试做的事情:

  • I open click on the 'Launch parent modal', I want the parent modal to show (This works great).我打开单击“启动父模式”,我希望显示父模式(这很好用)。
  • When I launch the child modal from inside the parent, I see the child modal (This works great).当我从父模式内部启动子模式时,我看到了子模式(这很好用)。
  • I want to be able to click on the close button inside the child modal and go back to the parent modal since it's still is-active , otherwise if they click anywhere in the shaded are it will close all the modals as it currently is doing.我希望能够单击子模态内的关闭按钮,然后 go 回到父模态,因为它仍然is-active ,否则如果他们单击阴影中的任何位置,它将像当前一样关闭所有模态。

So to sum it up, I just want the child modal to close so that I can see the parent modal only if the click on the close button.总而言之,我只想关闭子模式,以便只有在单击关闭按钮时才能看到父模式。

 document.addEventListener('DOMContentLoaded', function() { const rootEl = document.documentElement; const $modals = document.querySelectorAll('.modal'); const $modalButtons = document.querySelectorAll('.modal-button'); const $modalCloses = document.querySelectorAll('.modal-background, .modal-close, .modal-card-head.delete, .modal-card-foot.button'); if ($modalButtons.length > 0) { $modalButtons.forEach(function ($el) { $el.addEventListener('click', function () { var target = $el.dataset.target; openModal(target); }); }); } if ($modalCloses.length > 0) { $modalCloses.forEach(function ($el) { $el.addEventListener('click', function () { closeModals(); }); }); } function openModal(target) { var $target = document.getElementById(target); rootEl.classList.add('is-clipped'); $target.classList.add('is-active'); } function closeModals() { rootEl.classList.remove('is-clipped'); $modals.forEach(function ($el) { $el.classList.remove('is-active'); }); } document.addEventListener('keydown', function (event) { var e = event || window.event; if (e.keyCode === 27) { closeModals(); closeDropdowns(); } }) });
 <link href="https://cdn.jsdelivr.net/npm/bulma@0.9.1/css/bulma.min.css" rel="stylesheet"/> <button class="button is-primary is-large modal-button" data-target="modal-ter" aria-haspopup="true">Launch parent modal</button> <div id="modal-ter" class="modal"> <div class="modal-background"></div> <div class="modal-card"> <header class="modal-card-head"> <p class="modal-card-title">Modal title</p> <button class="delete" aria-label="close"></button> </header> <section class="modal-card-body"> <p> This is the parent modal (Scroll down and click the child modal button). </p> <button class="button is-primary is-large modal-button" data-target="modal-ter2" aria-haspopup="true">Launch child modal</button> </section> <footer class="modal-card-foot"> <button class="button is-success">Save changes</button> <button class="button">Cancel</button> </footer> </div> </div> <div id="modal-ter2" class="modal"> <div class="modal-background"></div> <div class="modal-card"> <header class="modal-card-head"> <p class="modal-card-title">Modal title</p> <button class="delete" aria-label="close"></button> </header> <section class="modal-card-body"> <p> This is the child modal </p> </section> <footer class="modal-card-foot"> <button class="button is-success">Save changes</button> <button class="button">Cancel</button> </footer> </div> </div>

I think you should bind different events for success and cancel butttons, cause they have different logics.我认为您应该为成功绑定不同的事件并取消按钮,因为它们具有不同的逻辑。

 document.addEventListener('DOMContentLoaded', function() { const rootEl = document.documentElement; const $modals = document.querySelectorAll('.modal'); const $modalButtons = document.querySelectorAll('.modal-button'); const $modalCloses = document.querySelectorAll('.modal-background, .modal-close, .modal-card-head.delete'); if ($modalButtons.length > 0) { $modalButtons.forEach(function ($el) { $el.addEventListener('click', function () { var target = $el.dataset.target; openModal(target); }); }); } if ($modalCloses.length > 0) { $modalCloses.forEach(function ($el) { $el.addEventListener('click', function () { closeModals(); }); }); } function openModal(target) { var $target = document.getElementById(target); rootEl.classList.add('is-clipped'); $target.classList.add('is-active'); } function closeModals() { rootEl.classList.remove('is-clipped'); $modals.forEach(function ($el) { $el.classList.remove('is-active'); }); } $modals.forEach(function ($el) { $el.addEventListener('click', function(event) { if (event.target.classList.contains('cancel')) { $el.classList.remove('is-active'); } }) }); document.addEventListener('keydown', function (event) { var e = event || window.event; if (e.keyCode === 27) { closeModals(); closeDropdowns(); } }) });
 <link href="https://cdn.jsdelivr.net/npm/bulma@0.9.1/css/bulma.min.css" rel="stylesheet"/> <button class="button is-primary is-large modal-button" data-target="modal-ter" aria-haspopup="true">Launch parent modal</button> <div id="modal-ter" class="modal"> <div class="modal-background"></div> <div class="modal-card"> <header class="modal-card-head"> <p class="modal-card-title">Modal title</p> <button class="delete" aria-label="close"></button> </header> <section class="modal-card-body"> <p> This is the parent modal (Scroll down and click the child modal button). </p> <button class="button is-primary is-large modal-button" data-target="modal-ter2" aria-haspopup="true">Launch child modal</button> </section> <footer class="modal-card-foot"> <button class="button is-success">Save changes</button> <button class="button cancel">Cancel</button> </footer> </div> </div> <div id="modal-ter2" class="modal"> <div class="modal-background"></div> <div class="modal-card"> <header class="modal-card-head"> <p class="modal-card-title">Modal title</p> <button class="delete" aria-label="close"></button> </header> <section class="modal-card-body"> <p> This is the child modal </p> </section> <footer class="modal-card-foot"> <button class="button is-success">Save changes</button> <button class="button cancel">Cancel</button> </footer> </div> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM