gpt4 book ai didi

javascript - 在滚动 jquery 上将 css 类切换为选中/未选中

转载 作者:行者123 更新时间:2023-11-28 05:49:49 25 4
gpt4 key购买 nike

当特定的 DIV 在滚动时出现时,我想将类“selected”添加到 [href]。并在 DIV 完全滚动时删除该类。此代码正在添加类,但在完全滚动后,a[href] 没有聚焦。

var aChildren = $(".cd-faq-categories li a"); // find the a children of the list items

var aArray = []; // create the empty aArray
for (var i = 0; i < aChildren.length; i++) {
var aChild = aChildren[i];
var ahref = $(aChild).attr('href');
aArray.push(ahref);
}

$(window).scroll(function () {
var windowPos = $(window).scrollTop(); // get the offset of the window from the top of page

var windowHeight = $(window).height(); // get the height of the window

var docHeight = $(document).height();

for (var i = 0; i < aArray.length; i++) {
var theID = aArray[i];
var divPos = $(theID).offset().top;
// get the offset of the div from the top of page
var divHeight = $(theID).height(); // get the height of the div in question

if (windowPos >= divPos && windowPos < (divPos + divHeight)) {
$("a[href='" + theID + "']").addClass("selected");
} else {
if ($("a[href='#cld9']"))
{
//...
}
else
{
$("a[href='" + theID + "']").removeClass("selected");
}
}
}
if (windowPos + windowHeight == docHeight) {
alert("gfg");
if (!$(".cd-faq-categories li a").hasClass("selected")) {
var navActiveCurrent = $("li").attr("href");
$("a[href='" + navActiveCurrent + "']").removeClass("selected");
$("li a").addClass("selected");
}
}
});

正确的写法是什么?

最佳答案

 } else {
if ($("a[href='#cld9']"))
{
//...
}
else
{
$("a[href='" + theID + "']").removeClass("selected");
}
}

我想你可能希望最后一行为所有其他 id 运行,你需要这样做

} else if ($("a[href='#cld9']")) {
//...
} else {
$("a[href='" + theID + "']").removeClass("selected");
}

代替

关于javascript - 在滚动 jquery 上将 css 类切换为选中/未选中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37405520/

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