2

在使用把手设置菜单时,我想检测与当前 URL 匹配的活动菜单项。

.active {
  color: red;
}
<a>1.1</a>
<br>
<a class="active">1.2</a>
<br>
<a>1.3</a>

因为目前我将菜单作为对象返回并将其传递给模板渲染器。我想我必须通过客户端代码来解决这个问题。此代码段工作正常

const setActiveSidebarLink = () => {
    const currentPagePath = window.location.pathname;
    const sidebarLinks = $(".sidebarLink");
    const activeSidebarLink = $(".sidebarLink[href$='" + currentPagePath + "']")[0];

// do something with activeSidebarLink
}

我可以用车把检查吗?

4

0 回答 0