gpt4 book ai didi

javascript - AddClass to parent
  • on 函数
  • 转载 作者:搜寻专家 更新时间:2023-10-31 22:03:14 24 4
    gpt4 key购买 nike

    我正在尝试向父级添加一个类 <li> ,这是我一直在尝试但没有结果的方法:

    function calendaractive(){
    $(this).parent('li').addClass("active");
    }

    HTML

    <li>
    <a href="javascript:void(0)" onclick="calendaractive();">2009</a>
    </li>

    为什么这不起作用?

    最佳答案

    试试吧。
    在您的函数绑定(bind)处传递 this

    <li>
    <a href="javascript:void(0)" onclick="calendaractive(this);">2009</a>
    </li>

    相应地更改 JS

    function calendaractive(anchorLink){
    $(anchorLink).parent().addClass("active");
    }

    关于javascript - AddClass to parent <li> on 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14726477/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com