【问题标题】:add or remove show active class on div在 div 上添加或删除显示活动类
【发布时间】:2022-03-05 01:03:25
【问题描述】:

如果单击 li,我想在 div 上添加显示活动,如果单击另一个 li,则将其删除

我有这个代码

<ul class="nav nav-tabs nav-pills nav-fill" id="postsTab" role="tablist">
                    <li class="nav-item" role="presentation"><button aria-controls="entertainment" aria-selected="true" class="nav-link active" data-bs-target="#entertainment" data-bs-toggle="tab" id="entertainment-tab" role="tab" type="button">Entertaiment</button></li>
                    <li class="nav-item" role="presentation"><button aria-controls="infotainment" aria-selected="false" class="nav-link" data-bs-target="#infotainment" data-bs-toggle="tab" id="infotainment-tab" role="tab" type="button">Infotainment</button></li>
                    <li class="nav-item" role="presentation"><button aria-controls="music" aria-selected="false" class="nav-link" data-bs-target="#music" data-bs-toggle="tab" id="music-tab" role="tab" type="button">Music</button></li>
                    <li class="nav-item" role="presentation"><button aria-controls="lifestyle" aria-selected="false" class="nav-link" data-bs-target="#lifestyle" data-bs-toggle="tab" id="lifestyle-tab" role="tab" type="button">Lifestyle</button></li>
                    <li class="nav-item" role="presentation"><button aria-controls="recipe" aria-selected="false" class="nav-link" data-bs-target="#recipe" data-bs-toggle="tab" id="recipe-tab" role="tab" type="button">Recipe</button></li>
                </ul>

并且对于类选项卡窗格淡入淡出上的添加-删除在下面的差异 div 上显示活动只有“选项卡窗格淡入淡出”

<!-- entertainment posts -->
                        <div aria-labelledby="entertainment-tab" class="tab-pane fade show active" id="entertainment" role="tabpanel">
                            <div class="row post-carousel-featured post-carousel">
                            @foreach($entertainment as $etrtmn)
                            <!-- post -->
                            ...

任何人都可以帮助我的代码?对不起我的英语不好

【问题讨论】:

标签: javascript php html css laravel


【解决方案1】:

const nav = document.getElementById('nav')

for (let child of nav.children) {
    child.addEventListener('click', function () {
        let current = document.getElementsByClassName('active')
        
        // If there's no active class
        if (current.length > 0) {
            current[0].className = current[0].className.replace(' active', '')
        }
        
        // Add the active class to the current/clicked button
        this.className += ' active'
    })
}
*{
    box-sizing: border-box;
 }

.nav-wrapper{
    max-width: 150px;
 }
 
.nav-item{
    background-color: lightblue;
    padding: .5rem;
    margin: 0 0 2px 0;
    list-style: none;
    text-align: center;
    cursor: pointer;
    
 }
 
.nav-item:hover{
    background-color: rgb(63, 94, 134); 
    color: whitesmoke;
 }

.active{
    border-left: .5rem solid tomato;
 }
<div class="nav-wrapper">
    <ul id="nav">
        <li class="nav-item active">Home</li>
        <li class="nav-item">About us</li>
        <li class="nav-item">Services</li>
        <li class="nav-item">Contact</li>
    </ul>
</div>

【讨论】:

  • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
  • 感谢此代码
  • 非常感谢您的反馈:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-11
  • 2023-01-22
  • 1970-01-01
  • 1970-01-01
  • 2013-06-25
相关资源
最近更新 更多