[英]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.