如何在当前菜单项页面上添加活动类? html [英] How to add an active class on a current menu item page? html
问题描述
如何在html header.php的当前菜单项页面上添加活动类. 我在wordpress中添加了一个自定义菜单:
How to add an active class on a current menu item page in html header.php . I added a custom menu in wordpress that is :
<ul class="menuul">
<li><a href="/#/" class="current">Начало</a></li>
<li><a href="/#/muskulnamasa/">Действие</a></li>
<li><a href="/#/sastavki/">Състав</a></li>
<li><a href="/#/testosteron/">Тестостерон</a></li>
<li><a href="/#/vuprosi/">FAQ</a></li>
<li><a href="/#/mnenia/">Мнения</a></li>
<li class="lastli"><a href="/#/porachka/">ПОРЪЧАЙ</a></li>
</ul>
那么如何在其中添加活动"呢? 可能带有一些javascript? 还是PHP? 我确实在网站上尝试了几个答案: 我确实尝试了此操作(无效)
So how to add "active" to it? may be with some javascript? or PHP? i did try several answers here in the website: i did try this(doesnt work)
这(没有成功); 最后是 似乎没有任何作用. 我可能在做一些本质上的错误-请帮助-谢谢!
and this(no success) ; and finally this Nothing seems to work. I am probably doing something essential wrong - please help - thanks!
PS:这也不起作用
回顾一下我需要什么:
我想在当前菜单项(当前页)中添加活动"类.不仅可以访问其中一个页面,还可以访问任何活动页面. 我需要一些东西来检测当前页面并向其添加一个类(在
I want to add class "active" to the current menu item(current page). Not only to one of the pages, but to any active . I need something to detect the current page and add a class to it(on the
推荐答案
只需添加当前答案,这就是我使用香草JavaScript的方式:
Just adding on current answers, this is how I did that with vanilla JavaScript:
const currentPage = location.href;
const allAs = document.querySelectorAll('a');
const allAsLength = allAs.length
for (let i = 0; i < allAsLength; i++) {
if ( allAs[i].href === currentPage ) {
allAs[i].className = "active";
}
}
这篇关于如何在当前菜单项页面上添加活动类? html的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!