我需要给一个元素与另一个元素相同的类

问题描述 投票:0回答:1
<div class='topnav' id='myTopnav'>
  <div class='dropdown'>
    <button class=' dropbtn'>Adobe &#x25BC;</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 正确时,我想为按钮提供与锚点标签相同的类。

javascript jquery menu
1个回答
0
投票

我已经很长时间没有使用 JQuery了,但这是我的建议。 你可以试试这个 首先找到衣柜

dropdown
,然后找到
dropbtn

$(this).closest('.dropdown').find('.dropbtn').addClass('active');

希望能帮到你

© www.soinside.com 2019 - 2024. All rights reserved.