gpt4 book ai didi

javascript - for 循环内的 jQuery 函数

转载 作者:行者123 更新时间:2023-12-03 10:32:44 26 4
gpt4 key购买 nike

假设我想在将鼠标悬停在相应的链接元素上时将段落元素变为红色。

为什么以下代码不起作用?最简单的替代方案是什么?

for (i = 0; i < 100; i++) {   
$("a").eq(i).mouseover(function(){
$("p").eq(i).css({"color":"red"})
})
}

需要简单的解释。

最佳答案

您不必循环遍历元素。这样做,

$("a").mouseover(function() {
$("p").eq($('a').index(this)).css("color", "red");
});

关于javascript - for 循环内的 jQuery 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29137333/

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