javascript - 如何使用javascript添加或删除li第一个孩子的类
问题描述
我想将 jquery 代码转换为 javascript,其中当窗口高度小于 50 时,添加或删除未在 li 第一个子级上添加的类,但添加或删除了其他 li 子类。在下面显示 jquery 代码
$(window).scroll(function(){
if ($(window).scrollTop() >=50) {
$('.responsive-header .social-icons .social-icon li').not(':first-child').addClass('hidden');
$('.responsive-header .social-icons .social-icon li').not(':first-child').removeClass('show');
}
});
解决方案
window.onscroll = function(e) {
if (e.target.scrollHeight > 50) {
document.querySelectorAll(".responsive - header.social - icons.social - icon li:not(:first-child)").forEach(function (el) {
el.classList.add('hidden');
el.classList.remove('show');
})
}
};
我没有测试它,但想法是这样的
推荐阅读
- facebook - WhatsApp Business API:“验证此电话号码时出错”
- linq - 如何在 LINQ 中获取重复值
- html - 导航栏项目不显示
- python - 在给定两个角的情况下裁剪任意维度的 NumPy 数组
- python - Python 用按键暂停游戏
- azure-data-factory-2 - 触发器中的夏令时更改(数据工厂 v2)
- python - 如何使用python中的for循环将字母添加到反向字符串
- c# - 如何在 Microsoft Graph API C# 中获取用户 ID 和访问令牌
- angular - Angular 6 JwtInterceptor
- r - 从 purrr 获取地图以使用 paste0