英文:
why the functionality of the tabs not working?
问题
这是一段HTML代码,定义了一个带有id“alltabs”和class“allTabs”的部分。在这个部分里,有一个包含三个按钮的容器,它们具有class“tab-btn”和id“tab1”、“tab2”和“tab3”。第一个按钮具有class“active”,表示它是默认的活动选项卡。
每个按钮代表一个功能,当用户点击其中一个时,会激活相应的选项卡,它具有与按钮相同的id。选项卡包含一张图片和一篇文章,文章包括标题、一段文本和一个具有class“tab-btn-more-info”的按钮。
这段代码的目的很可能是创建一个用户界面,用于网站或应用程序,允许用户通过提供诸如一键式书签、智能搜索和简单共享等功能来管理他们的书签。选项卡系统提供了一种在不凌乱界面的情况下展示和切换不同功能的方式。
英文:
I'm a beginner web developer learning front end development. Recently I was working on a project from front end mentor, It has a functionality which, if the name of a tab is clicked the content will be updated. I tried, But it's not working.
<section class="features">
<div>
<h2>Features</h2>
<p>Our aim is to make it quick and easy for you to access your favourite websites.
Your bookmarks sync between your devices so you can access them on the go.</p>
</div>
</section>
<section id="allTabs" class="allTabs">
<div class="btn-container">
<button class="tab-btn active" id="tab1">Simple Bookmarking</button>
<button class="tab-btn" id="tab2">Speedy Searching</button>
<button class="tab-btn" id="tab3">Easy Sharing</button>
</div>
<div class="tab active" id="tab1">
<img class="tab-img" src="./images/illustration-features-tab-1.svg" alt="">
<article>
<h3 class="tab-title">Bookmark in one click</h3>
<p class="tab-text">Organize your bookmarks however you like. Our simple drag-and-drop interface
gives you complete control over how you manage your favourite sites.</p>
<button class="tab-btn-more-info">More Info</button>
</article>
</div>
<div class="tab" id="tab2">
<img class="tab-img" src="./images/illustration-features-tab-2.svg" alt="">
<article>
<h3 class="tab-title">Intelligent search</h3>
<p class="tab-text">Our powerful search feature will help you find saved sites in no time at all.
No need to trawl through all of your bookmarks.</p>
<button class="tab-btn-more-info">More Info</button>
</article>
</div>
<div class="tab" id="tab3">
<img class="tab-img" src="./images/illustration-features-tab-3.svg" alt="">
<article>
<h3 class="tab-title">Share your bookmarks</h3>
<p class="tab-text">Easily share your bookmarks and collections with others. Create a shareable
link that you can send at the click of a button.</p>
<button class="tab-btn-more-info">More Info</button>
</article>
</div>
</section>
.tab {
display: none;
}
.tab-btn.active {
border-bottom: 3px solid hsl(0, 94%, 66%);
}
.tab.active {
display: flex;
margin-left: 4.2rem;
margin-right: 4.2rem;
}
const allTabs = document.querySelector(".allTabs");
const btnsAll = document.querySelectorAll(".tab-btn");
const articles = document.querySelectorAll(".tab");
allTabs.addEventListener("click", function (e) {
const id = e.target.dataset.id;
if (id) {
// remove active from other buttons
btnsAll.forEach(function (btn) {
btn.classList.remove("active");
e.target.classList.add("active");
});
// hide other all articles
articles.forEach(function (article) {
article.classList.remove("active");
});
const element = document.getElementById(id);
element.classList.add("active");
}
});
This is the code. Can anyone fix this.This CSS code adds styles to the active tabs and their corresponding articles. When a tab is clicked, the .active class is added to both the .tab-btn button and the .tab article, which triggers the styles specified in this CSS code.
The styles included in this code are:
border-bottom: 3px solid hsl(0, 94%, 66%);
: Adds a colored bottom border to the active tab button to indicate which tab is currently selected.
margin-left: 4.2rem; margin-right: 4.2rem; display: flex;
: Adds some margin to the left and right of the active tab article, and sets its display to flex to allow for more flexible positioning.
display: block; justify-content: left;
: Sets the display to block for the active article and aligns its content to the left.
text-align: left; display: block; margin-top: 6rem; padding-left: 3rem;
: Adds some margin and padding to the left of the active article's title and aligns it to the left.
text-align: left; padding-left: 1.4rem;
: Aligns the active article's text to the left and adds some padding to its left.
display: block; margin-left: 3rem; margin-top: 2rem;
: Positions the "More Info" button for the active article by adding some margin to its left and top.
This is a piece of HTML code that defines a section with the id "alltabs" and the class "allTabs". Inside this section, there is a container with three buttons that have the class "tab-btn" and the ids "tab1", "tab2", and "tab3". The first button has the class "active" which indicates it is the default active tab.
Each button represents a feature and when a user clicks on one of them, it activates the corresponding tab, which has the same id as the button. The tab contains an image and an article with a title, a paragraph of text, and a button with the class "tab-btn-more-info".
The purpose of this code is likely to create a user interface for a website or application that allows users to manage their bookmarks by providing features such as one-click bookmarking, intelligent search, and easy sharing. The tab system provides a way to present and switch between different features without cluttering the interface.
答案1
得分: 1
唯一的错误是您在`buttons`上使用了`id`而不是`data-id`,所以应该是
[`CODESANDBOX DEMO`][1]
<div class="btn-container">
// 将 id 改为 data-id
<button class="tab-btn active" data-id="tab1">简单的书签</button>
<button class="tab-btn" data-id="tab2">快速搜索</button>
<button class="tab-btn" data-id="tab3">轻松分享</button>
</div>
[1]: https://codesandbox.io/s/xenodochial-cache-67bjeg?file=/index.html
英文:
The only mistake that you are doing is you are using id
instead of data-id
on the buttons
so it should be
<div class="btn-container">
// CHANGE data-id instead of id
<button class="tab-btn active" data-id="tab1">Simple Bookmarking</button>
<button class="tab-btn" data-id="tab2">Speedy Searching</button>
<button class="tab-btn" data-id="tab3">Easy Sharing</button>
</div>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论