我需要在悬停时向 li
添加一个类,但从列表中的所有其他 li
中删除相同的类(这是用于导航)。
我目前的代码是:
jQuery(".navcontent").on('mouseenter', 'li', function() {
if (jQuery('.childmenu', this)) {
jQuery('.childmenu', this).addClass("child-active");
} else {
jQuery('.navcontent li .childmenu').removeClass("child-active");
}
});
我不太明白我需要做什么...
如有任何帮助,我们将不胜感激。
最佳答案
您有其他方法可以做到这一点:
- 首先删除所有元素的类并添加一个你想要的:
jQuery(".navcontent li").on('mouseenter', function() { jQuery('.childmenu').removeClass("child-active"); jQuery('.childmenu', this).addClass("child-active"); });
- 如果他们是 sibling ,你可以使用
siblings
:
jQuery(".navcontent li").on('mouseenter', function() { jQuery('.childmenu', this).addClass("child-active") .siblings('.childmenu').removeClass("child-active"); });
关于javascript - 在悬停时向 li 添加类,但从所有其他类中删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31606079/