英文:
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.
<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('');
})
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('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(); // Solution: Prevent submission only if invalid form
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>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论