<div class='topnav' id='myTopnav'>
<div class='dropdown'>
<button class=' dropbtn'>Adobe ▼</button>
<div class='dropdown-content'>
<a href='index'>Photoshop</a>
</div>
</div>
</div>
<script>
jQuery(function($) {
var path = window.location.href;
$('#myTopnav a').each(function() {
if (this.href === path) {
$(this).addClass('active');
$(this).parent().parent('first-child').addClass('active');
}
});
});
</script>
我不太擅长 jQuery 甚至 Javascript,但当锚点指向的 url 正确时,我想为按钮提供与锚点标签相同的类。
我已经很长时间没有使用 JQuery了,但这是我的建议。 你可以试试这个 首先找到衣柜
dropdown
,然后找到dropbtn
$(this).closest('.dropdown').find('.dropbtn').addClass('active');
希望能帮到你