只在我尝试切换的两个部分中的一个部分起作用。

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

Hidden class only working on one of the two sections that I'm trying to toggle

问题

我在处理一个前端挑战,创建一个新闻简讯注册页面。我在一个主要标签内包装了注册页面和成功消息,使用了一个名为 .hidden 的类,并创建了一个函数,在用户点击注册页面上的订阅按钮时切换隐藏类,以及在成功页面上的关闭按钮被点击后切换隐藏类,当用户成功注册后。我的问题是切换只对成功消息起作用;如果用户成功注册,消息会出现,当用户点击关闭按钮时会关闭,但注册部分一直可见。

<div class="signup-pg" id="signup-page">
      <div class="col col-img">
        <picture class="layout-img">
          <source srcset="./assets/images/illustration-sign-up-desktop.svg" media="(min-width: 770px)">
          <img src="./assets/images/illustration-sign-up-mobile.svg" alt="">
        </picture>
      </div>
      <div class="col col-content">

        <h1>Stay updated!</h1>
        <p class="lead">
          Join 60,000+ product managers receiving monthly updates on:
        </p>
        <ul role="list">
          <li>
            <span class="list-icon"><img src="./assets/images/icon-list.svg" alt=""></span>
            <p class="list-item">
            Product discovery and building what matters
            </p>
          </li>
          <li>
            <span class="list-icon"><img src="./assets/images/icon-list.svg" alt=""></span>
            <p class="list-item">
              Measuring to ensure updates are a success
            </p>
          </li>
          <li>
            <span class="list-icon"><img src="./assets/images/icon-list.svg" alt=""></span>
            <p class="list-item">
              And much more!
            </p>
          </li>
        </ul>
      
      <!-- Sign-up form start -->
      <form action="/success.html" id="signup-form">
        <fieldset>
          <legend>Email address</legend>
          <label for="mail">
            <input class="email-input" type="email" id="email" name="usermail" placeholder="email@company.com" required>
            <p class="error-message hidden">Valid email required</p>
            <button class="btn" type="submit" id="submitBtn">Subscribe to monthly newsletter</button>
            
          </label>
        </fieldset>
      </form>
    </div>
  </div>
.hidden {
  display: none;
}
const signupForm = document.getElementById('signup-form');
const userEmail = document.getElementById('user-email');
const signUpPage = document.getElementById('signup-page');
const successPage = document.getElementById('success-pg');
const dismiss = document.getElementById('dismiss')

function updateSuccessMsg(email) {
    userEmail.textContent = email;
}

function toggleMainAndSuccess() {
    signUpPage.classList.toggle('hidden');
    successPage.classList.toggle('hidden');
}

function isValidEntry(str) {
    const reg = new RegExp('[a-z0-9]+@[a-z]+.[a-z]{2,3}');
    return reg.test(str);
  }

function showErrors() {
    const errorMsg = document.getElementById('error-message');
    const input = document.getElementById('email');
    errorMsg.classList.remove('hidden');
    input.classList.add('error');
}

signupForm.addEventListener('submit', (e) => {
    e.preventDefault();
    const email = signupForm.querySelector('#email')
    const valid = isValidEntry(email.value);
    if (!valid) {
        return showErrors();
    }
    else if (valid) {
        updateSuccessMsg(email.value);
        toggleMainAndSuccess();
    }
});

dismiss.addEventListener('click', () => {
    toggleMainAndSuccess();
    updateSuccessMsg('');
})

我的初步想法是我可能在类名上犯了一个拼写错误,所以 JavaScript 没有按照我打算的方式应用。但后来我检查了开发者工具,当我点击订阅按钮时,隐藏类显示在我希望隐藏的 div 上,但用户仍然可以看到它。

英文:

I am working on a frontend mentor challenge creating a newsletter sign up page. I wrapped the sign up page and the success message in separate divs inside a main tag, a .hidden class, and created a function to toggle the hidden class when the subscribe button is clicked on the sign up page, and when the dismiss button is clicked on the success page after a user signs up.
My issue is that the toggle is only working for the success message; the message appears if the user signs up successfully and closes when the user clicks the dismiss button, but the sign up portion stays visible the whole time.

&lt;div class=&quot;signup-pg&quot; id=&quot;signup-page&quot;&gt;
      &lt;div class=&quot;col col-img&quot;&gt;
        &lt;picture class=&quot;layout-img&quot;&gt;
          &lt;source srcset=&quot;./assets/images/illustration-sign-up-desktop.svg&quot; media=&quot;(min-width: 770px)&quot;&gt;
          &lt;img src=&quot;./assets/images/illustration-sign-up-mobile.svg&quot; alt=&quot;&quot;&gt;
        &lt;/picture&gt;
      &lt;/div&gt;
      &lt;div class=&quot;col col-content&quot;&gt;
  
        &lt;h1&gt;Stay updated!&lt;/h1&gt;
        &lt;p class=&quot;lead&quot;&gt;
          Join 60,000+ product managers receiving monthly updates on:
        &lt;/p&gt;
        &lt;ul role=&quot;list&quot;&gt;
          &lt;li&gt;
            &lt;span class=&quot;list-icon&quot;&gt;&lt;img src=&quot;./assets/images/icon-list.svg&quot; alt=&quot;&quot;&gt;&lt;/span&gt;
            &lt;p class=&quot;list-item&quot;&gt;
            Product discovery and building what matters
            &lt;/p&gt;
          &lt;/li&gt;
          &lt;li&gt;
            &lt;span class=&quot;list-icon&quot;&gt;&lt;img src=&quot;./assets/images/icon-list.svg&quot; alt=&quot;&quot;&gt;&lt;/span&gt;
            &lt;p class=&quot;list-item&quot;&gt;
              Measuring to ensure updates are a success
            &lt;/p&gt;
          &lt;/li&gt;
          &lt;li&gt;
            &lt;span class=&quot;list-icon&quot;&gt;&lt;img src=&quot;./assets/images/icon-list.svg&quot; alt=&quot;&quot;&gt;&lt;/span&gt;
            &lt;p class=&quot;list-item&quot;&gt;
              And much more!
            &lt;/p&gt;
          &lt;/li&gt;
        &lt;/ul&gt;
      
      &lt;!-- Sign-up form start --&gt;
      &lt;form action=&quot;/success.html&quot; id=&quot;signup-form&quot;&gt;
        &lt;fieldset&gt;
          &lt;legend&gt;Email address&lt;/legend&gt;
          &lt;label for=&quot;mail&quot;&gt;
            &lt;input class=&quot;email-input&quot; type=&quot;email&quot; id=&quot;email&quot; name=&quot;usermail&quot; placeholder=&quot;email@company.com&quot; required&gt;
            &lt;p class=&quot;error-message hidden&quot;&gt;Valid email required&lt;/p&gt;
            &lt;button class=&quot;btn&quot; type=&quot;submit&quot; id=&quot;submitBtn&quot; &gt;Subscribe to monthly newsletter&lt;/button&gt;
            
          &lt;/label&gt;
        &lt;/fieldset&gt;
      &lt;/form&gt;
    &lt;/div&gt;
  &lt;/div&gt;
.hidden {
  display: none;
}
const signupForm = document.getElementById(&#39;signup-form&#39;);
const userEmail = document.getElementById(&#39;user-email&#39;);
const signUpPage = document.getElementById(&#39;signup-page&#39;);
const successPage = document.getElementById(&#39;success-pg&#39;);
const dismiss = document.getElementById(&#39;dismiss&#39;)

function updateSuccessMsg(email) {
    userEmail.textContent = email;
}

function toggleMainAndSuccess() {
    signUpPage.classList.toggle(&#39;hidden&#39;);
    successPage.classList.toggle(&#39;hidden&#39;);
}

function isValidEntry(str) {
    const reg = new RegExp(&#39;[a-z0-9]+@[a-z]+.[a-z]{2,3}&#39;);
    return reg.test(str);
  }

function showErrors() {
    const errorMsg = document.getElementById(&#39;error-message&#39;);
    const input = document.getElementById(&#39;email&#39;);
    errorMsg.classList.remove(&#39;hidden&#39;);
    input.classList.add(&#39;error&#39;);
}

signupForm.addEventListener(&#39;submit&#39;, (e) =&gt; {
    e.preventDefault();
    const email = signupForm.querySelector(&#39;#email&#39;)
    const valid = isValidEntry(email.value);
    if (!valid) {
        return showErrors();
    }
    else if (valid) {
        updateSuccessMsg(email.value);
        toggleMainAndSuccess();
    }
});

dismiss.addEventListener(&#39;click&#39;, () =&gt; {
    toggleMainAndSuccess();
    updateSuccessMsg(&#39;&#39;);
})

My initial thought was that I made a typo in my class name, so the javascript wasn't applied the way I intended it to. But then I checked the developer tools, and when I click the subscribe button the class hidden shows up on the div that I want hidden, but the user can still see it.

答案1

得分: 1

如你所说,类被添加了,但仍然可见,这意味着隐藏类的复杂性比其他选择器低。

.hidden{
  display:none !important;
 }

> 使用 !important 将覆盖其他选择器。

英文:

As you said, the classes is adding but still visible that means the hidden class has less complexity than other selector

.hidden{
display:none !important;
}

> Use !important will override any other selectors

答案2

得分: 0

在代码中缺少一个成功页面的HTML,所以我在这里添加了它。除此之外,代码运行正常。请通过运行它来检查。

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

<!-- language: lang-js -->
const signupForm = document.getElementById('signup-form');
const userEmail = document.getElementById('user-email');
const signUpPage = document.getElementById('signup-page');
const successPage = document.getElementById('success-pg');
const dismiss = document.getElementById('dismiss')

function updateSuccessMsg(email) {
  userEmail.textContent = email;
}

function toggleMainAndSuccess() {
  signUpPage.classList.toggle('hidden');
  successPage.classList.toggle('hidden');
}

function isValidEntry(str) {
  const reg = new RegExp('[a-z0-9]+@[a-z]+.[a-z]{2,3}');
  return reg.test(str);
}

function showErrors() {
  const errorMsg = document.getElementById('error-message');
  const input = document.getElementById('email');
  errorMsg.classList.remove('hidden');
  input.classList.add('error');
}

signupForm.addEventListener('submit', (e) => {
  const email = signupForm.querySelector('#email')
  const valid = isValidEntry(email.value);
  if (!valid) {
    e.preventDefault(); // 解决方案: 仅在表单无效时阻止提交
    return showErrors();
  }
});

dismiss.addEventListener('click', () => {
  toggleMainAndSuccess();
  updateSuccessMsg('');
})

<!-- language: lang-css -->
.hidden {
  display: none;
}

<!-- language: lang-html -->
<div class="signup-pg" id="signup-page">
  <div class="col col-img">
    <picture class="layout-img">
      <source srcset="./assets/images/illustration-sign-up-desktop.svg" media="(min-width: 770px)">
      <img src="./assets/images/illustration-sign-up-mobile.svg" alt="">
    </picture>
  </div>
  <div class="col col-content">

    <h1>Stay updated!</h1>
    <p class="lead">
      Join 60,000+ product managers receiving monthly updates on:
    </p>
    <ul role="list">
      <li>
        <span class="list-icon"><img src="./assets/images/icon-list.svg" alt=""></span>
        <p class="list-item">
          Product discovery and building what matters
        </p>
      </li>
      <li>
        <span class="list-icon"><img src="./assets/images/icon-list.svg" alt=""></span>
        <p class="list-item">
          Measuring to ensure updates are a success
        </p>
      </li>
      <li>
        <span class="list-icon"><img src="./assets/images/icon-list.svg" alt=""></span>
        <p class="list-item">
          And much more!
        </p>
      </li>
    </ul>

    <!-- Sign-up form start -->
    <form action="/success.html" id="signup-form">
      <fieldset>
        <legend>Email address</legend>
        <label for="mail">
          <input class="email-input" type="email" id="email" name="usermail" placeholder="email@company.com" required>
          <p class="error-message hidden">Valid email required</p>
          <button class="btn" type="submit" id="submitBtn">Subscribe to monthly newsletter</button>
        </label>
      </fieldset>
    </form>
  </div>
</div>

<div id="success-pg" class="hidden">
  <p> <span id="user-email"></span> This is a success message.</p>
  <button id="dismiss">x</button>
</div>

<!-- 结束代码片段 -->
英文:

You were missing a success page html in the code. So I added it here. Other than that, the code is working fine. Please check by running it.

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

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

const signupForm = document.getElementById(&#39;signup-form&#39;);
const userEmail = document.getElementById(&#39;user-email&#39;);
const signUpPage = document.getElementById(&#39;signup-page&#39;);
const successPage = document.getElementById(&#39;success-pg&#39;);
const dismiss = document.getElementById(&#39;dismiss&#39;)
function updateSuccessMsg(email) {
userEmail.textContent = email;
}
function toggleMainAndSuccess() {
signUpPage.classList.toggle(&#39;hidden&#39;);
successPage.classList.toggle(&#39;hidden&#39;);
}
function isValidEntry(str) {
const reg = new RegExp(&#39;[a-z0-9]+@[a-z]+.[a-z]{2,3}&#39;);
return reg.test(str);
}
function showErrors() {
const errorMsg = document.getElementById(&#39;error-message&#39;);
const input = document.getElementById(&#39;email&#39;);
errorMsg.classList.remove(&#39;hidden&#39;);
input.classList.add(&#39;error&#39;);
}
signupForm.addEventListener(&#39;submit&#39;, (e) =&gt; {
const email = signupForm.querySelector(&#39;#email&#39;)
const valid = isValidEntry(email.value);
if (!valid) {
e.preventDefault(); // Solution: Prevent submission only if invalid form
return showErrors();
}
});
dismiss.addEventListener(&#39;click&#39;, () =&gt; {
toggleMainAndSuccess();
updateSuccessMsg(&#39;&#39;);
})

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

.hidden {
display: none;
}

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

&lt;div class=&quot;signup-pg&quot; id=&quot;signup-page&quot;&gt;
&lt;div class=&quot;col col-img&quot;&gt;
&lt;picture class=&quot;layout-img&quot;&gt;
&lt;source srcset=&quot;./assets/images/illustration-sign-up-desktop.svg&quot; media=&quot;(min-width: 770px)&quot;&gt;
&lt;img src=&quot;./assets/images/illustration-sign-up-mobile.svg&quot; alt=&quot;&quot;&gt;
&lt;/picture&gt;
&lt;/div&gt;
&lt;div class=&quot;col col-content&quot;&gt;
&lt;h1&gt;Stay updated!&lt;/h1&gt;
&lt;p class=&quot;lead&quot;&gt;
Join 60,000+ product managers receiving monthly updates on:
&lt;/p&gt;
&lt;ul role=&quot;list&quot;&gt;
&lt;li&gt;
&lt;span class=&quot;list-icon&quot;&gt;&lt;img src=&quot;./assets/images/icon-list.svg&quot; alt=&quot;&quot;&gt;&lt;/span&gt;
&lt;p class=&quot;list-item&quot;&gt;
Product discovery and building what matters
&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;span class=&quot;list-icon&quot;&gt;&lt;img src=&quot;./assets/images/icon-list.svg&quot; alt=&quot;&quot;&gt;&lt;/span&gt;
&lt;p class=&quot;list-item&quot;&gt;
Measuring to ensure updates are a success
&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;span class=&quot;list-icon&quot;&gt;&lt;img src=&quot;./assets/images/icon-list.svg&quot; alt=&quot;&quot;&gt;&lt;/span&gt;
&lt;p class=&quot;list-item&quot;&gt;
And much more!
&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;!-- Sign-up form start --&gt;
&lt;form action=&quot;/success.html&quot; id=&quot;signup-form&quot;&gt;
&lt;fieldset&gt;
&lt;legend&gt;Email address&lt;/legend&gt;
&lt;label for=&quot;mail&quot;&gt;
&lt;input class=&quot;email-input&quot; type=&quot;email&quot; id=&quot;email&quot; name=&quot;usermail&quot; placeholder=&quot;email@company.com&quot; required&gt;
&lt;p class=&quot;error-message hidden&quot;&gt;Valid email required&lt;/p&gt;
&lt;button class=&quot;btn&quot; type=&quot;submit&quot; id=&quot;submitBtn&quot; &gt;Subscribe to monthly newsletter&lt;/button&gt;
&lt;/label&gt;
&lt;/fieldset&gt;
&lt;/form&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&quot;success-pg&quot; class=&quot;hidden&quot;&gt;
&lt;p&gt; &lt;span id=&quot;user-email&quot;&gt;&lt;/span&gt; This is a success message.&lt;/p&gt;
&lt;button id=&quot;dismiss&quot;&gt;x&lt;/button&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月26日 09:44:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76553083.html
匿名

发表评论

匿名网友

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

确定