弹出模态框问题

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

Pop-up Modal Issue

问题

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

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

<!-- 开始代码片段: js 隐藏: false 控制台: true babel: false -->

<!-- 语言: lang-js -->
const modal = document.querySelector("#modal");
const openModal = document.querySelector(".open-button");
const closeModal = document.querySelector(".close-button");

openModal.addEventListener("click", () => {
  modal.showModal();
});

closeModal.addEventListener("click", () => {
  modal.close();
});

<!-- 语言: lang-css -->
.modal {
    padding: 5em;
    max-width: 100ch;
}

.modal2 {
    padding: 1em;
    margin: 5em;
    max-width: 100ch;
}

.modal3 {
    padding: 1em;
    max-width: 50ch;
}

<!-- 语言: lang-html -->
<dialog class="modal" id="modal">
    <h2>银行项目信息</h2>
    <p>我曾经参与过一个小组,我们创建了一个在线银行,用户可以借款和存款。他们可以创建自己的账户,自由登录和注销。</p>
    <button class="button close-button">关闭</button>
</dialog>

<dialog class="modal2" id="modal2">
    <h2>学校项目信息</h2>
    <p>我和一些同学一起创建了一个学校项目,我们创建了一个控制台应用程序,可以向应用程序添加学生和老师。我们可以添加学生信息,了解他们的老师以及每个班级有多少学生。</p>
    <button class="button close-button">关闭</button>
</dialog>

<dialog class="modal3" id="modal3">
    <h2>ASP.NET项目信息</h2>
    <p>我们被要求为一个网店构建一个管理应用程序,这个应用程序应该有一个专门的管理员功能。我和我的小组创建了一个非常有趣的网页,我们在项目中使用了HTML、CSS和JavaScript。</p>
    <button class="button close-button">关闭</button>
</dialog>

<!-- 结束代码片段 -->

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

英文:

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 -->

    const modal = document.querySelector(&quot;#modal&quot;);
    const openModal = document.querySelector(&quot;.open-button&quot;);
    const closeModal = document.querySelector(&quot;.close-button&quot;);
    
    openModal.addEventListener(&quot;click&quot;, () =&gt; {
      modal.showModal();
    });
    
    closeModal.addEventListener(&quot;click&quot;, () =&gt; {
      modal.close();
    });

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

    .modal {
        padding: 5em;
        max-width: 100ch;
       
    }
    .modal2 {
        padding: 1em;
        margin: 5em;
        max-width: 100ch;
      
    }
    .modal3 {
        padding: 1em;
        max-width: 50ch;
    
    }

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

    &lt;dialog class=&quot;modal&quot; id=&quot;modal&quot;&gt;
        &lt;h2&gt;Bank Project Info&lt;/h2&gt;
        &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;             
        &lt;button class=&quot;button close-button&quot;&gt;close&lt;/button&gt;
    &lt;/dialog&gt;
    &lt;dialog class=&quot;modal&quot; id=&quot;modal2&quot;&gt;
        &lt;h2&gt;School Project Info&lt;/h2&gt;
        &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;                       
        &lt;button class=&quot;button close-button&quot;&gt;close&lt;/button&gt;
    &lt;/dialog&gt;



     &lt;dialog class=&quot;modal&quot; id=&quot;modal3&quot;&gt;
        &lt;h2&gt;ASP.NET Project Info&lt;/h2&gt;
        &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;                       
        &lt;button class=&quot;button close-button&quot;&gt;close&lt;/button&gt;
    &lt;/dialog&gt;

<!-- end snippet -->

答案1

得分: 1

以下是代码部分的翻译:

// 获取所有打开模态框的按钮
const openButtons = document.querySelectorAll("[data-opens]");

// 循环遍历按钮
openButtons.forEach(function(btn) {
  // 获取选择器
  var modalSelector = btn.dataset.opens;

  btn.addEventListener("click", function() {
    document.querySelector(modalSelector).showModal();
  });
});

// 获取所有关闭按钮并附加事件
const closeButtons = document.querySelectorAll(".close-button");

closeButtons.forEach(function(btn) {
  btn.addEventListener("click", function() {
    // 查找最近的模态框元素并关闭它
    btn.closest('.modal').close();
  });
});
.modal {
  padding: 5em;
  max-width: 100ch;
}

.modal2 {
  padding: 1em;
  margin: 5em;
  max-width: 100ch;
}

.modal3 {
  padding: 1em;
  max-width: 50ch;
}
<dialog class="modal" id="modal">
  <h2>银行项目信息</h2>
  <p>我曾参与一个小组项目,我们创建了一个在线银行,用户可以借款和存款。他们可以创建自己的帐户,自由登录和退出。</p>
  <button class="button close-button">关闭</button>
</dialog>
<dialog class="modal" id="modal2">
  <h2>学校项目信息</h2>
  <p>我和一些同学一起创建了一个控制台应用程序,允许我们添加学生和教师信息。我们可以添加学生信息,了解他们的老师是谁,以及每个班级有多少学生。</p>
  <button class="button close-button">关闭</button>
</dialog>
<dialog class="modal" id="modal3">
  <h2>ASP.NET 项目信息</h2>
  <p>我们被要求为一个网店创建一个管理应用程序,它应该具有单独的管理员功能。我和我的团队创建了一个非常有趣的网页,我们使用了HTML、CSS和JavaScript来完成这个项目。</p>
  <button class="button close-button">关闭</button>
</dialog>
<button data-opens="#modal">1</button>
<button data-opens="#modal2">2</button>
<button data-opens="#modal3">3</button>
// 事件委托的另一种解决方案
document.body.addEventListener("click", function(evt) {
  // 获取被点击的元素
  const elem = event.target;

  // 判断点击的是打开按钮还是关闭按钮
  if (elem.matches('[data-opens]')) {
    var modalSelector = elem.dataset.opens;
    document.querySelector(modalSelector).showModal();
  } else if (elem.matches('.close-button')) {
    elem.closest(".modal").close();
  }
});

希望这对你有所帮助。

英文:

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 -->

// get all the buttons that open the modals
const openButtons = document.querySelectorAll(&quot;[data-opens]&quot;);

// loop over the buttons
openButtons.forEach(function(btn) {
  // get the selector
  var modalSelector = btn.dataset.opens;

  btn.addEventListener(&quot;click&quot;, function() {
    document.querySelector(modalSelector).showModal();
  });
});

//get all the close buttons and attach events
// get all the buttons that open the modals
const closeButtons = document.querySelectorAll(&quot;.close-button&quot;);

closeButtons.forEach(function(btn) {
  btn.addEventListener(&quot;click&quot;, function() {
    // find the parent that is a modal element and close it
    btn.closest(&#39;.modal&#39;).close();
  });
});

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

.modal {
  padding: 5em;
  max-width: 100ch;
}

.modal2 {
  padding: 1em;
  margin: 5em;
  max-width: 100ch;
}

.modal3 {
  padding: 1em;
  max-width: 50ch;
}

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

&lt;dialog class=&quot;modal&quot; id=&quot;modal&quot;&gt;
  &lt;h2&gt;Bank Project Info&lt;/h2&gt;
  &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;
  &lt;button class=&quot;button close-button&quot;&gt;close&lt;/button&gt;
&lt;/dialog&gt;
&lt;dialog class=&quot;modal&quot; id=&quot;modal2&quot;&gt;
  &lt;h2&gt;School Project Info&lt;/h2&gt;
  &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;
  &lt;button class=&quot;button close-button&quot;&gt;close&lt;/button&gt;
&lt;/dialog&gt;



&lt;dialog class=&quot;modal&quot; id=&quot;modal3&quot;&gt;
  &lt;h2&gt;ASP.NET Project Info&lt;/h2&gt;
  &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;
  &lt;button class=&quot;button close-button&quot;&gt;close&lt;/button&gt;
&lt;/dialog&gt;


&lt;button data-opens=&quot;#modal&quot;&gt;1&lt;/button&gt;
&lt;button data-opens=&quot;#modal2&quot;&gt;2&lt;/button&gt;
&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 -->

// Bind event to an element that contains all of the modals and open buttons
document.body.addEventListener(&quot;click&quot;, function(evt) {
  // get was clicked
  const elem = event.target;

  // determine if we clicked the open button or the close button
  if (elem.matches(&#39;[data-opens]&#39;)) {
    var modalSelector = elem.dataset.opens;
    document.querySelector(modalSelector).showModal();
  } else if (elem.matches(&#39;.close-button&#39;)) {
    elem.closest(&quot;.modal&quot;).close();
  }
});

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

.modal {
  padding: 5em;
  max-width: 100ch;
}

.modal2 {
  padding: 1em;
  margin: 5em;
  max-width: 100ch;
}

.modal3 {
  padding: 1em;
  max-width: 50ch;
}

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

&lt;dialog class=&quot;modal&quot; id=&quot;modal&quot;&gt;
  &lt;h2&gt;Bank Project Info&lt;/h2&gt;
  &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;
  &lt;button class=&quot;button close-button&quot;&gt;close&lt;/button&gt;
&lt;/dialog&gt;
&lt;dialog class=&quot;modal&quot; id=&quot;modal2&quot;&gt;
  &lt;h2&gt;School Project Info&lt;/h2&gt;
  &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;
  &lt;button class=&quot;button close-button&quot;&gt;close&lt;/button&gt;
&lt;/dialog&gt;



&lt;dialog class=&quot;modal&quot; id=&quot;modal3&quot;&gt;
  &lt;h2&gt;ASP.NET Project Info&lt;/h2&gt;
  &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;
  &lt;button class=&quot;button close-button&quot;&gt;close&lt;/button&gt;
&lt;/dialog&gt;


&lt;button data-opens=&quot;#modal&quot;&gt;1&lt;/button&gt;
&lt;button data-opens=&quot;#modal2&quot;&gt;2&lt;/button&gt;
&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:

确定