弹出模态框问题

huangapple go评论98阅读模式
英文:

Pop-up Modal Issue

问题

我尝试为我的简历页面创建了一个弹出式模态框,只有第一个弹出式模态框有效,而第二个和第三个模态框在我尝试单击每个模态框的按钮时没有弹出。我尝试为模态框赋予“id”唯一的数字,但没有成功,我还尝试替换最后两个模态框的名称,但也没有成功。

我应该实现一个onClick函数吗?还是我漏掉了什么?

  1. <!-- 开始代码片段: js 隐藏: false 控制台: true babel: false -->
  2. <!-- 语言: lang-js -->
  3. const modal = document.querySelector("#modal");
  4. const openModal = document.querySelector(".open-button");
  5. const closeModal = document.querySelector(".close-button");
  6. openModal.addEventListener("click", () => {
  7. modal.showModal();
  8. });
  9. closeModal.addEventListener("click", () => {
  10. modal.close();
  11. });
  12. <!-- 语言: lang-css -->
  13. .modal {
  14. padding: 5em;
  15. max-width: 100ch;
  16. }
  17. .modal2 {
  18. padding: 1em;
  19. margin: 5em;
  20. max-width: 100ch;
  21. }
  22. .modal3 {
  23. padding: 1em;
  24. max-width: 50ch;
  25. }
  26. <!-- 语言: lang-html -->
  27. <dialog class="modal" id="modal">
  28. <h2>银行项目信息</h2>
  29. <p>我曾经参与过一个小组,我们创建了一个在线银行,用户可以借款和存款。他们可以创建自己的账户,自由登录和注销。</p>
  30. <button class="button close-button">关闭</button>
  31. </dialog>
  32. <dialog class="modal2" id="modal2">
  33. <h2>学校项目信息</h2>
  34. <p>我和一些同学一起创建了一个学校项目,我们创建了一个控制台应用程序,可以向应用程序添加学生和老师。我们可以添加学生信息,了解他们的老师以及每个班级有多少学生。</p>
  35. <button class="button close-button">关闭</button>
  36. </dialog>
  37. <dialog class="modal3" id="modal3">
  38. <h2>ASP.NET项目信息</h2>
  39. <p>我们被要求为一个网店构建一个管理应用程序,这个应用程序应该有一个专门的管理员功能。我和我的小组创建了一个非常有趣的网页,我们在项目中使用了HTML、CSS和JavaScript。</p>
  40. <button class="button close-button">关闭</button>
  41. </dialog>
  42. <!-- 结束代码片段 -->

希望这有助于解决你的问题。

英文:

I tried creating a pop-up modal for my resume page and only my first pop-up modal worked, both modal 2 and 3 didn't not pop-up when I tried clicking on the button for each modal. I tried giving the modal "id" unique numbers but that didn't work out and I tried replacing the names for last 2 modals but it didn't work.

Should I implement a onClick function or what am I missing?

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

  1. const modal = document.querySelector(&quot;#modal&quot;);
  2. const openModal = document.querySelector(&quot;.open-button&quot;);
  3. const closeModal = document.querySelector(&quot;.close-button&quot;);
  4. openModal.addEventListener(&quot;click&quot;, () =&gt; {
  5. modal.showModal();
  6. });
  7. closeModal.addEventListener(&quot;click&quot;, () =&gt; {
  8. modal.close();
  9. });

<!-- language: lang-css -->

  1. .modal {
  2. padding: 5em;
  3. max-width: 100ch;
  4. }
  5. .modal2 {
  6. padding: 1em;
  7. margin: 5em;
  8. max-width: 100ch;
  9. }
  10. .modal3 {
  11. padding: 1em;
  12. max-width: 50ch;
  13. }

<!-- language: lang-html -->

  1. &lt;dialog class=&quot;modal&quot; id=&quot;modal&quot;&gt;
  2. &lt;h2&gt;Bank Project Info&lt;/h2&gt;
  3. &lt;p&gt;I have been a part of a group were we created a online bank in which users could go in and borrow and deposit money. They could create their own account and log in and out on their own accord.&lt;/p&gt;
  4. &lt;button class=&quot;button close-button&quot;&gt;close&lt;/button&gt;
  5. &lt;/dialog&gt;
  6. &lt;dialog class=&quot;modal&quot; id=&quot;modal2&quot;&gt;
  7. &lt;h2&gt;School Project Info&lt;/h2&gt;
  8. &lt;p&gt;Me and some other classmates created a school project were we created a console application that enabled us to add both students and teacher into the application. We could add student information, who their teachers were and how many students each class had.&lt;/p&gt;
  9. &lt;button class=&quot;button close-button&quot;&gt;close&lt;/button&gt;
  10. &lt;/dialog&gt;
  11. &lt;dialog class=&quot;modal&quot; id=&quot;modal3&quot;&gt;
  12. &lt;h2&gt;ASP.NET Project Info&lt;/h2&gt;
  13. &lt;p&gt;We were tasked to build a administration application for a webshop, it was suppose to be a create with a seperate admin functionality for the website. Was a very interesting webpage that me and my group created, we used HTML, CSS and JavaScript for the project.&lt;/p&gt;
  14. &lt;button class=&quot;button close-button&quot;&gt;close&lt;/button&gt;
  15. &lt;/dialog&gt;

<!-- end snippet -->

答案1

得分: 1

以下是代码部分的翻译:

  1. // 获取所有打开模态框的按钮
  2. const openButtons = document.querySelectorAll("[data-opens]");
  3. // 循环遍历按钮
  4. openButtons.forEach(function(btn) {
  5. // 获取选择器
  6. var modalSelector = btn.dataset.opens;
  7. btn.addEventListener("click", function() {
  8. document.querySelector(modalSelector).showModal();
  9. });
  10. });
  11. // 获取所有关闭按钮并附加事件
  12. const closeButtons = document.querySelectorAll(".close-button");
  13. closeButtons.forEach(function(btn) {
  14. btn.addEventListener("click", function() {
  15. // 查找最近的模态框元素并关闭它
  16. btn.closest('.modal').close();
  17. });
  18. });
  1. .modal {
  2. padding: 5em;
  3. max-width: 100ch;
  4. }
  5. .modal2 {
  6. padding: 1em;
  7. margin: 5em;
  8. max-width: 100ch;
  9. }
  10. .modal3 {
  11. padding: 1em;
  12. max-width: 50ch;
  13. }
  1. <dialog class="modal" id="modal">
  2. <h2>银行项目信息</h2>
  3. <p>我曾参与一个小组项目,我们创建了一个在线银行,用户可以借款和存款。他们可以创建自己的帐户,自由登录和退出。</p>
  4. <button class="button close-button">关闭</button>
  5. </dialog>
  6. <dialog class="modal" id="modal2">
  7. <h2>学校项目信息</h2>
  8. <p>我和一些同学一起创建了一个控制台应用程序,允许我们添加学生和教师信息。我们可以添加学生信息,了解他们的老师是谁,以及每个班级有多少学生。</p>
  9. <button class="button close-button">关闭</button>
  10. </dialog>
  11. <dialog class="modal" id="modal3">
  12. <h2>ASP.NET 项目信息</h2>
  13. <p>我们被要求为一个网店创建一个管理应用程序,它应该具有单独的管理员功能。我和我的团队创建了一个非常有趣的网页,我们使用了HTML、CSS和JavaScript来完成这个项目。</p>
  14. <button class="button close-button">关闭</button>
  15. </dialog>
  16. <button data-opens="#modal">1</button>
  17. <button data-opens="#modal2">2</button>
  18. <button data-opens="#modal3">3</button>
  1. // 事件委托的另一种解决方案
  2. document.body.addEventListener("click", function(evt) {
  3. // 获取被点击的元素
  4. const elem = event.target;
  5. // 判断点击的是打开按钮还是关闭按钮
  6. if (elem.matches('[data-opens]')) {
  7. var modalSelector = elem.dataset.opens;
  8. document.querySelector(modalSelector).showModal();
  9. } else if (elem.matches('.close-button')) {
  10. elem.closest(".modal").close();
  11. }
  12. });

希望这对你有所帮助。

英文:

Your code is only running on one element you need to have code for each one.

The copy/paste solution is make multiple copies for each button. That is not great.

Better solution is code that loops over html collection and binds events.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

  1. // get all the buttons that open the modals
  2. const openButtons = document.querySelectorAll(&quot;[data-opens]&quot;);
  3. // loop over the buttons
  4. openButtons.forEach(function(btn) {
  5. // get the selector
  6. var modalSelector = btn.dataset.opens;
  7. btn.addEventListener(&quot;click&quot;, function() {
  8. document.querySelector(modalSelector).showModal();
  9. });
  10. });
  11. //get all the close buttons and attach events
  12. // get all the buttons that open the modals
  13. const closeButtons = document.querySelectorAll(&quot;.close-button&quot;);
  14. closeButtons.forEach(function(btn) {
  15. btn.addEventListener(&quot;click&quot;, function() {
  16. // find the parent that is a modal element and close it
  17. btn.closest(&#39;.modal&#39;).close();
  18. });
  19. });

<!-- language: lang-css -->

  1. .modal {
  2. padding: 5em;
  3. max-width: 100ch;
  4. }
  5. .modal2 {
  6. padding: 1em;
  7. margin: 5em;
  8. max-width: 100ch;
  9. }
  10. .modal3 {
  11. padding: 1em;
  12. max-width: 50ch;
  13. }

<!-- language: lang-html -->

  1. &lt;dialog class=&quot;modal&quot; id=&quot;modal&quot;&gt;
  2. &lt;h2&gt;Bank Project Info&lt;/h2&gt;
  3. &lt;p&gt;I have been a part of a group were we created a online bank in wich users could go in and borrow and deposit money. They could create their own account and log in and out on their own accord.&lt;/p&gt;
  4. &lt;button class=&quot;button close-button&quot;&gt;close&lt;/button&gt;
  5. &lt;/dialog&gt;
  6. &lt;dialog class=&quot;modal&quot; id=&quot;modal2&quot;&gt;
  7. &lt;h2&gt;School Project Info&lt;/h2&gt;
  8. &lt;p&gt;Me and some other classmates created a school project were we created a console application that enabled us to add both students and teacher into the application. We could add student information, who their teachers were and how many students each class
  9. had.&lt;/p&gt;
  10. &lt;button class=&quot;button close-button&quot;&gt;close&lt;/button&gt;
  11. &lt;/dialog&gt;
  12. &lt;dialog class=&quot;modal&quot; id=&quot;modal3&quot;&gt;
  13. &lt;h2&gt;ASP.NET Project Info&lt;/h2&gt;
  14. &lt;p&gt;We were tasked to build a administration application for a webshop, it was suppose to be a create with a seperate admin functionality for the website. Was a very interesting webpage that me and my group created, we used HTML, CSS and JavaScript for
  15. the project.&lt;/p&gt;
  16. &lt;button class=&quot;button close-button&quot;&gt;close&lt;/button&gt;
  17. &lt;/dialog&gt;
  18. &lt;button data-opens=&quot;#modal&quot;&gt;1&lt;/button&gt;
  19. &lt;button data-opens=&quot;#modal2&quot;&gt;2&lt;/button&gt;
  20. &lt;button data-opens=&quot;#modal3&quot;&gt;3&lt;/button&gt;

<!-- end snippet -->

Other solution is event delegation

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

  1. // Bind event to an element that contains all of the modals and open buttons
  2. document.body.addEventListener(&quot;click&quot;, function(evt) {
  3. // get was clicked
  4. const elem = event.target;
  5. // determine if we clicked the open button or the close button
  6. if (elem.matches(&#39;[data-opens]&#39;)) {
  7. var modalSelector = elem.dataset.opens;
  8. document.querySelector(modalSelector).showModal();
  9. } else if (elem.matches(&#39;.close-button&#39;)) {
  10. elem.closest(&quot;.modal&quot;).close();
  11. }
  12. });

<!-- language: lang-css -->

  1. .modal {
  2. padding: 5em;
  3. max-width: 100ch;
  4. }
  5. .modal2 {
  6. padding: 1em;
  7. margin: 5em;
  8. max-width: 100ch;
  9. }
  10. .modal3 {
  11. padding: 1em;
  12. max-width: 50ch;
  13. }

<!-- language: lang-html -->

  1. &lt;dialog class=&quot;modal&quot; id=&quot;modal&quot;&gt;
  2. &lt;h2&gt;Bank Project Info&lt;/h2&gt;
  3. &lt;p&gt;I have been a part of a group were we created a online bank in wich users could go in and borrow and deposit money. They could create their own account and log in and out on their own accord.&lt;/p&gt;
  4. &lt;button class=&quot;button close-button&quot;&gt;close&lt;/button&gt;
  5. &lt;/dialog&gt;
  6. &lt;dialog class=&quot;modal&quot; id=&quot;modal2&quot;&gt;
  7. &lt;h2&gt;School Project Info&lt;/h2&gt;
  8. &lt;p&gt;Me and some other classmates created a school project were we created a console application that enabled us to add both students and teacher into the application. We could add student information, who their teachers were and how many students each class
  9. had.&lt;/p&gt;
  10. &lt;button class=&quot;button close-button&quot;&gt;close&lt;/button&gt;
  11. &lt;/dialog&gt;
  12. &lt;dialog class=&quot;modal&quot; id=&quot;modal3&quot;&gt;
  13. &lt;h2&gt;ASP.NET Project Info&lt;/h2&gt;
  14. &lt;p&gt;We were tasked to build a administration application for a webshop, it was suppose to be a create with a seperate admin functionality for the website. Was a very interesting webpage that me and my group created, we used HTML, CSS and JavaScript for
  15. the project.&lt;/p&gt;
  16. &lt;button class=&quot;button close-button&quot;&gt;close&lt;/button&gt;
  17. &lt;/dialog&gt;
  18. &lt;button data-opens=&quot;#modal&quot;&gt;1&lt;/button&gt;
  19. &lt;button data-opens=&quot;#modal2&quot;&gt;2&lt;/button&gt;
  20. &lt;button data-opens=&quot;#modal3&quot;&gt;3&lt;/button&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年3月31日 20:13:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/75898416.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定