簡體   English   中英

將我的 Bootstrap 導航欄鏈接設置為活動狀態(使用純 JavaScript)

[英]Set my Bootstrap navbar links to be active (with pure JavaScript)

我試圖讓我的導航欄鏈接在點擊時變為活動狀態。 我想我會通過在點擊時添加活動類來做到這一點; 但是,我不確定如何阻止當前活動鏈接處於活動狀態。 另外,我不確定

let links = document.querySelectorAll('.nav-link');

links.addEventListener('click', function() {
    this.classList.add('active');
});

另外,我有這個來激活鏈接; 但是,在 html 運行后我無法讓它運行,所以它不起作用。

https://jsfiddle.net/ysejr17L/2/

TL; 博士

  • 不知道如何讓 JS 找到當前活動的鏈接並刪除它的活動類
  • 用於在 HTML 之前使點擊鏈接活動加載的代碼,因此它不起作用

看看這個片段:

 let links = document.querySelectorAll('.nav-link'); for(let i=0; i<links.length; i++){ links[i].addEventListener('click', function() { for(let j=0; j<links.length; j++) links[j].classList.remove('active'); this.classList.add('active'); }); }
 body { background: linear-gradient(0.1turn, #673fa6, #e1f5f8, #f6c43c); } nav { opacity: 0.8; } .text-shadow { text-shadow: 5px 5px 15px; }
 <!doctype html> <html lang="en"> <head> <!-- Required meta tags --> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> <!-- Bootstrap CSS --> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous"> <!-- My CSS --> <link rel="stylesheet" href="styles/style.css"> <title>Justice Dunn</title> </head> <body> <nav class="navbar navbar-dark bg-dark fixed-top"> <a class="navbar-brand" href="#"> <img src="node_modules/bootstrap-icons/icons/kanban-fill.svg" width="30" height="30" class="d-inline-block align-top" alt="" loading="lazy"> Justice Dunn </a> <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNavAltMarkup" aria-controls="navbarNavAltMarkup" aria-expanded="false" aria-label="Toggle navigation"> <span class="navbar-toggler-icon"></span> </button> <div class="collapse navbar-collapse" id="navbarNavAltMarkup"> <div class="navbar-nav"> <a class="nav-link active" href="#bg1">Home</a> <a class="nav-link" href="#bg2">Background</a> <a class="nav-link" href="#bg3">Hobbies</a> <a class="nav-link" href="#bg4">Achievements</a> <a class="nav-link" href="#bg5">The Future</a> <a class="nav-link" href="#bg6">Gallery</a> </div> </div> </nav> <div class="container vh-100 vw-100 d-flex align-items-center" id="bg1"> <div class="container d-flex-inline justify-content-center"> <h1 class="text-center text-shadow display-3">Hello!</h1> <p class="text-center text-shadow h4">I'm Justice Dunn, a 19-year-old developer in Cheyenne, Wyoming.</p> </div> </div> <div class="container vh-100 vw-100 d-flex align-items-center" id="bg2"> <div class="container d-flex-inline justify-content-center"> <h1 class="text-center text-black text-shadow display-3">Background</h1> <p class="text-center text-black mt-3 h5">I decided that I wanted to become a developer for a few reasons.</p> <p class="text-center text-black mt-3 h5">I've always been a creative person, and I've always enjoyed problem solving. Being a developer just seems to be an occupation that naturally incorporates both of those things.</p> </div> </div> <div class="container vh-100 vw-100 d-flex align-items-center" id="bg3"> <div class="container d-flex-inline justify-content-center"> <h1 class="text-center text-black text-shadow display-3">Hobbies</h1> <p>Writing Music</p> <p>Digital Art</p> <p>3D Modeling</p> </div> </div> <div class="container vh-100 vw-100 d-flex align-items-center" id="bg4"> <div class="container d-flex-inline justify-content-center"> <h1 class="text-center text-black text-shadow display-3">Achievements</h1> <p class="text-center h5 mt-3">I don't have a lot of life experience yet, so I'd say my greatest achievement was getting accepted into the Array apprenticeship program!</p> </div> </div> <div class="container vh-100 vw-100 d-flex align-items-center" id="bg5"> <div class="container d-flex-inline justify-content-center"> <h1 class="text-center text-black text-shadow display-3">The Future</h1> <p class="text-center h5 mt-3">After this Array apprenticeship, I want to begin developing apps and write, record, and produce an album.</p> </div> </div> <div class="container vh-100 vw-100 d-flex align-items-center" id="bg6"> <div class="container d-flex-inline justify-content-center"> <h1 class="text-center text-black text-shadow display-3">Gallery</h1> </div> </div> <!-- Optional JavaScript --> <!-- jQuery first, then Popper.js, then Bootstrap JS and local JS --> <script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script> <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.1/dist/umd/popper.min.js" integrity="sha384-9/reFTGAW83EW2RDu2S0VKaIzap3H66lZH81PoYlFhbGU+6BZp6G7niu735Sk7lN" crossorigin="anonymous"></script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js" integrity="sha384-B4gt1jrGC7Jh4AgTPSdUtOBvfO8shuf57BaghqFfPlYxofvL8/KUEfYiJOMMV+rV" crossorigin="anonymous"></script> <script src="/scripts/script.js"></script> </body> </html>

我們正在為.nav-link類的每個元素添加點擊事件監聽器,它首先從所有元素中刪除.active類,然后將類.active添加到被點擊的元素。

您也可以嘗試此操作,而無需在每次加載時使用循環。

 document.body.addEventListener('click', function(e) { if(e.target.className==='nav-link'){ document.getElementsByClassName('nav-link active')[0].classList.remove('active'); e.target.classList.add('active'); } });
 body { background: linear-gradient(0.1turn, #673fa6, #e1f5f8, #f6c43c); } nav { opacity: 0.8; } .text-shadow { text-shadow: 5px 5px 15px; }
 <!doctype html> <html lang="en"> <head> <!-- Required meta tags --> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> <!-- Bootstrap CSS --> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous"> <!-- My CSS --> <link rel="stylesheet" href="styles/style.css"> <title>Justice Dunn</title> </head> <body> <nav class="navbar navbar-dark bg-dark fixed-top"> <a class="navbar-brand" href="#"> <img src="node_modules/bootstrap-icons/icons/kanban-fill.svg" width="30" height="30" class="d-inline-block align-top" alt="" loading="lazy"> Justice Dunn </a> <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNavAltMarkup" aria-controls="navbarNavAltMarkup" aria-expanded="false" aria-label="Toggle navigation"> <span class="navbar-toggler-icon"></span> </button> <div class="collapse navbar-collapse" id="navbarNavAltMarkup"> <div class="navbar-nav"> <a class="nav-link active" href="#bg1">Home</a> <a class="nav-link" href="#bg2">Background</a> <a class="nav-link" href="#bg3">Hobbies</a> <a class="nav-link" href="#bg4">Achievements</a> <a class="nav-link" href="#bg5">The Future</a> <a class="nav-link" href="#bg6">Gallery</a> </div> </div> </nav> <div class="container vh-100 vw-100 d-flex align-items-center" id="bg1"> <div class="container d-flex-inline justify-content-center"> <h1 class="text-center text-shadow display-3">Hello!</h1> <p class="text-center text-shadow h4">I'm Justice Dunn, a 19-year-old developer in Cheyenne, Wyoming.</p> </div> </div> <div class="container vh-100 vw-100 d-flex align-items-center" id="bg2"> <div class="container d-flex-inline justify-content-center"> <h1 class="text-center text-black text-shadow display-3">Background</h1> <p class="text-center text-black mt-3 h5">I decided that I wanted to become a developer for a few reasons.</p> <p class="text-center text-black mt-3 h5">I've always been a creative person, and I've always enjoyed problem solving. Being a developer just seems to be an occupation that naturally incorporates both of those things.</p> </div> </div> <div class="container vh-100 vw-100 d-flex align-items-center" id="bg3"> <div class="container d-flex-inline justify-content-center"> <h1 class="text-center text-black text-shadow display-3">Hobbies</h1> <p>Writing Music</p> <p>Digital Art</p> <p>3D Modeling</p> </div> </div> <div class="container vh-100 vw-100 d-flex align-items-center" id="bg4"> <div class="container d-flex-inline justify-content-center"> <h1 class="text-center text-black text-shadow display-3">Achievements</h1> <p class="text-center h5 mt-3">I don't have a lot of life experience yet, so I'd say my greatest achievement was getting accepted into the Array apprenticeship program!</p> </div> </div> <div class="container vh-100 vw-100 d-flex align-items-center" id="bg5"> <div class="container d-flex-inline justify-content-center"> <h1 class="text-center text-black text-shadow display-3">The Future</h1> <p class="text-center h5 mt-3">After this Array apprenticeship, I want to begin developing apps and write, record, and produce an album.</p> </div> </div> <div class="container vh-100 vw-100 d-flex align-items-center" id="bg6"> <div class="container d-flex-inline justify-content-center"> <h1 class="text-center text-black text-shadow display-3">Gallery</h1> </div> </div> <!-- Optional JavaScript --> <!-- jQuery first, then Popper.js, then Bootstrap JS and local JS --> <script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script> <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.1/dist/umd/popper.min.js" integrity="sha384-9/reFTGAW83EW2RDu2S0VKaIzap3H66lZH81PoYlFhbGU+6BZp6G7niu735Sk7lN" crossorigin="anonymous"></script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js" integrity="sha384-B4gt1jrGC7Jh4AgTPSdUtOBvfO8shuf57BaghqFfPlYxofvL8/KUEfYiJOMMV+rV" crossorigin="anonymous"></script> <script src="/scripts/script.js"></script> </body> </html>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM