当按钮被点击时如何更改模态框内容

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

How to change modal content when button is clicked

问题

Sure, here's the translated part of your text:

我在我的代码中有2个模态框(box1和box2),box2位于box1之上。它们都有按钮。我如何使我的模态框1首先出现,然后只有在单击其按钮后内容才会更改为box2(使box1消失)?在我的原始代码中,实际上有4个模态框,但我以后会进行适应。如果可能的话,我希望这个内容过渡是平滑的。
请在全屏中打开代码片段,以便模态框显示出来。

I have excluded the code part as requested.

英文:

I have 2 modals (box1 and box2) in my code box2 is on top of box1. Both of them have buttons. How can I make my modal 1 appears first and then only when you click his button the content changes to box2 (making box1 disappear)? In my original code I have 4 modals actually but i'll addapt to it later. Also if is possible I would like that this content transition was smooth.
OBS: Please open the code snippet in full page so that the modal shows up

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

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

    .conteudo {
    width: 100%;
    height: 100%;
    top: 0;
    left: 0;
    background: rgba(0, 0, 0, 0.5);
    position: fixed;
    right: 0;
    bottom: 0;
    display: flex;
    justify-content: center;
}

.box, .box2 {
    width: 100%;
    max-width: 500px;
    background: #fff;
    position: absolute;
    bottom: 0;
    height: 400px;
}

.lineButton button {
    font-size: 14px;
    text-align: center;
    border: 2px solid black;
    padding: 16px;
    border-radius: 16px;
}

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

    &lt;body&gt;
    &lt;div class=&quot;conteudo&quot;&gt;
        &lt;div class=&quot;box&quot;&gt;            
            &lt;div class=&quot;boxes&quot;&gt;
                &lt;div class=&quot;modalTxt&quot;&gt;
                    &lt;h1&gt;MODAL 1 &lt;br&gt;Accept?&lt;/h1&gt;
                    &lt;div class=&quot;lineButton&quot;&gt;
                        &lt;button type=&quot;submit&quot; class=&quot;accept1&quot;&gt;Accept&lt;/button&gt;
                    &lt;/div&gt;                           
                &lt;/div&gt;         
            &lt;/div&gt;
        &lt;/div&gt;
        &lt;div class=&quot;box2&quot;&gt;            
            &lt;div class=&quot;boxes&quot;&gt;
                &lt;div class=&quot;modalTxt&quot;&gt;
                    &lt;h1&gt;MODAL 2 &lt;br&gt;Accept?&lt;/h1&gt;
                    &lt;div class=&quot;lineButton&quot;&gt;
                        &lt;button type=&quot;submit&quot; class=&quot;accept2&quot;&gt;Accept&lt;/button&gt;
                    &lt;/div&gt;                           
                &lt;/div&gt;         
            &lt;/div&gt;
        &lt;/div&gt;
    &lt;/div&gt;
&lt;/body&gt;

<!-- end snippet -->

答案1

得分: 1

将按钮添加addEventListener以监听click事件。然后,添加一个CSS类来隐藏元素,并将所有需要隐藏的元素添加到该类中。最后,从要显示的元素中移除该类,并将其添加到需要隐藏的元素中。

const BUTTON_1 = document.querySelector('.accept1');
const BOX_1 = document.querySelector('.box');
const BOX_2 = document.querySelector('.box2');

BUTTON_1.addEventListener('click', function() {
  BOX_1.classList.add('d-none');
  BOX_2.classList.remove('d-none');
});
/* 添加的CSS */
.d-none {
  display: none;
}

这些是要添加的代码部分,用于实现所描述的功能。

英文:

Add an addEventListener to the button to listen for a click event. Then add a class to CSS to hide elements and give all the elements that should be hidden in that class. Last but not least remove the class from the element you want to show and add it to the element you need to hide.

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

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

const BUTTON_1 = document.querySelector(&#39;.accept1&#39;);
const BOX_1 = document.querySelector(&#39;.box&#39;);
const BOX_2 = document.querySelector(&#39;.box2&#39;);

BUTTON_1.addEventListener(&#39;click&#39;, function() {
  BOX_1.classList.add(&#39;d-none&#39;);
  BOX_2.classList.remove(&#39;d-none&#39;);
})

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

.conteudo {
  width: 100%;
  height: 100%;
  top: 0;
  left: 0;
  background: rgba(0, 0, 0, 0.5);
  position: fixed;
  right: 0;
  bottom: 0;
  display: flex;
  justify-content: center;
}

.box,
.box2 {
  width: 100%;
  max-width: 500px;
  background: #fff;
  position: absolute;
  bottom: 0;
  height: 400px;
}

.lineButton button {
  font-size: 14px;
  text-align: center;
  border: 2px solid black;
  padding: 16px;
  border-radius: 16px;
}

/* added CSS */
.d-none {
  display: none;
}

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

&lt;div class=&quot;conteudo&quot;&gt;
  &lt;div class=&quot;box&quot;&gt;
    &lt;div class=&quot;boxes&quot;&gt;
      &lt;div class=&quot;modalTxt&quot;&gt;
        &lt;h1&gt;MODAL 1 &lt;br&gt;Accept?&lt;/h1&gt;
        &lt;div class=&quot;lineButton&quot;&gt;
          &lt;button type=&quot;submit&quot; class=&quot;accept1&quot;&gt;Accept&lt;/button&gt;
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
  &lt;div class=&quot;box2 d-none&quot;&gt;
    &lt;div class=&quot;boxes&quot;&gt;
      &lt;div class=&quot;modalTxt&quot;&gt;
        &lt;h1&gt;MODAL 2 &lt;br&gt;Accept?&lt;/h1&gt;
        &lt;div class=&quot;lineButton&quot;&gt;
          &lt;button type=&quot;submit&quot; class=&quot;accept2&quot;&gt;Accept&lt;/button&gt;
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月10日 21:17:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76218929.html
匿名

发表评论

匿名网友

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

确定