gpt4 book ai didi

javascript - 有没有另一种写第6行的方法

转载 作者:行者123 更新时间:2023-11-30 13:54:43 24 4
gpt4 key购买 nike

我是jQuery新手,不熟悉第6行代码的写法,只是想简单的写一下理解(应该是选择id为blockID的"data-scroll")

$(window).scroll(function(){
$(".block").each(function(){
if ($(window).scrollTop() > $(this).offset().top){
var blockID = $(this).attr("id");
$("nav a").removeClass("active");
$('nav a[data-scroll="' + blockID + '"]').addClass("active");
}
});
});

最佳答案

您可以使用 Template literals但正如@msg 指出的那样,这不是特定于 jQuery 的。

$(window).scroll(function(){
$(".block").each(function(){
if ($(window).scrollTop() > $(this).offset().top){
var blockID = $(this).attr("id");
$("nav a").removeClass("active");
$(`nav a[data-scroll="${blockID }"]`).addClass("active");
}
});
});

关于javascript - 有没有另一种写第6行的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57533761/

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