gpt4 book ai didi

javascript - 使用普通 JS 突出显示事件选项卡

转载 作者:行者123 更新时间:2023-11-30 14:23:10 24 4
gpt4 key购买 nike

我正在创建一个选项卡式导航栏,其中当选项卡处于事件状态时,它的颜色应该更改为我设置的更改颜色。使用选项卡浏览页面效果很好,但事件选项卡上的颜色突出显示似乎不起作用。

到目前为止,这是我的代码:

HTML:

<section class="tab" id="active_Current_Tabs">
<header>Active/Current Tabs</header>
<nav class="navbar">
<ul>
<li class="btn currentTab" onclick="activeTab(event, 'lorem7')">TAB1</li>
<li class="btn currentTab" onclick="activeTab(event, 'lorem8')">TAB2</li>
<li class="btn currentTab" onclick="activeTab(event, 'lorem9')">TAB3</li>
</ul>
</nav>
<main class="main-doc">
<article class="selectedPage" id='lorem7'>
<h2>Lorem</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Repellendus, est?</p>
</article>
<article class="selectedPage" id="lorem8">
<h2>Lorem</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Repellendus, est?</p>
</article>
<article class="selectedPage" id="lorem9">
<h2>Lorem</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Repellendus, est?</p>
</article>
</main>
</section>

CSS:

article {
text-align: center;
width: 100%;
height: 300px;
max-height: 300px;
margin: 0;
}

/*navbar css*/

nav {
width: 100%;
height: 75px;
padding: 0;
margin: 0;
}

nav ul {
height: 100%;
padding: 0;
display: flex;
list-style: none;
justify-content: space-around;
}

.btn {
display: flex;
align-items: center;
justify-content: center;
text-align: center;
width: 100%;
height: 100%;
background-color: #8b9d98;
cursor: pointer;
font-weight: 500;
}

.btn:hover {
background-color: #d7e0e0;
font-weight: 700;
transition: .5s;
}

/*main css*/
main {
margin-top: 0;
}


/*Active/Current Tab */

#lorem7 {
display: flex;
flex-direction: column;
background-color: #49c2a4;
align-items: center;
justify-content: center;
}

#lorem8 {
display: none;
flex-direction: column;
background-color:#35386f;
align-items: center;
justify-content: center;
}

#lorem9 {
display: none;
flex-direction: column;
background-color:#e28968;
align-items: center;
justify-content: center;
}

Javascript:

// active/current tab function

function activeTab(evnt, currPage) {
var currenttab;
var pages = document.getElementsByClassName('selectedPage');
for (i = 0; i < pages.length; i++) {
pages[i].style.display = "none";
}
//for dehighlighting inactive tabs
currenttab = document.getElementsByClassName('currentTab');
for(j = 0; j < currenttab.length; j++) {
currenttab[j].className = currenttab[j].className.replace("green", " ");
}
document.getElementById(currPage).style.display = "flex";
evnt.currentTarget.className += "green"; //this appends the color to active tab
}

请帮忙! T_T

最佳答案

我不确定您尝试对“绿色”类做什么,因为您的 CSS 中没有针对它的规则。我假设您希望事件选项卡与事件页面颜色相同来回答这个问题。抱歉,如果这不是您想要的,但我认为这是有道理的。

为避免特定类名出现问题,您可以使用 .classList 方法,如“添加”和“删除”。这样您就不必担心标记中类名的顺序。示例:

tabs[i].classList.remove('active')
e.currentTarget.classList.add('active')

您还可以动态附加事件监听器(点击处理程序)以保持 HTML 整洁。示例:

for(j = 0; j < tabs.length; j++) {
// attach event listener to all tabs
tabs[j].addEventListener('click', clickTab)
}

您还可以通过将相似的样式分配给公共(public)类来减少 CSS 的重复性:

.page {display:none;}
.page.active {
display:flex;
flex-direction: column;
align-items: center;
justify-content: center;
}

我修改了您的 ID,以便能够独立引用选项卡和页面,而无需明确将参数传递给点击处理函数。示例:

<li id="t2" class="tab">TAB2</li>
...
<article class="page" id="p2">...</article>

这是我的 JS Bin:

http://jsbin.com/defidih/edit?html,css,js,console,output

关于javascript - 使用普通 JS 突出显示事件选项卡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52388416/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com