gpt4 book ai didi

javascript - 无法循环 JQuery .hover()

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

当你想在 JQuery .hover() 方法上运行循环时,它会遇到问题。假设我有如下代码

<div class="screen screen_1"></div>
<div class="screen screen_2"></div>
<div class="screen screen_3"></div>
for(j=0; j<$('.screen').length+1; j++){
$('.screen_'+ j +'').hover(
function mousein(){
//do something when mouse enters

}, function mouseout(){
//do something else when mouse leaves
}
);
}

现在 jhint 告诉我不要在循环内执行函数,所以我有以下内容,但它仍然不喜欢它。

var mousein = function(){
//do something when mouse enters
};

var mouseout = function(){
//do something else when mouse leaves
};

for(j=0; j<$('.screen').length+1; j++){
$('.screen_'+ j +'').hover(
mousein, mouseout;
);
}

提前致谢

最佳答案

不需要循环...

 $('.screen').hover(
function(){
//do something when mouse enters
// $(this) gets you the element being affected.
}, function(){
//do something else when mouse leaves
}
);

关于javascript - 无法循环 JQuery .hover(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44296696/

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