我正在尝试向父级添加一个类 <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/