[英]Active font-weight with JS + CSS
我需要知道如何在頁面上激活字體粗細(我正在使用 JavaScript)。
示例:如果我在教師頁面上, <a> Teachers </a>
字體粗體應該如下圖所示為粗體。
<!DOCTYPE html>
<html>
<head>
{% block head %}
<title>Private Classes</title>
{% endblock %}
<link rel="stylesheet" href="/styles.css">
</head>
<body>
<header>
<div class="links">
<a href="/" class="active">Teachers</a>
<a href="/students">Students</a>
</div>
</header>
{% block content %}
{% endblock %}
</body>
</html>
您在教師頁面上,並且“活動” class 已經存在,因此無需為此使用 javascript。 只需將字體粗細添加到“活動” class 就足夠了。
<style>
.active{
font-weight: bold;
}
</style>
const currentPage = location.pathname
const menuItems = document.querySelectorAll("header .links a")
for (item of menuItems) {
if (currentPage.includes(item.getAttribute("href"))) {
item.classList.add("active")
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.