setInterval 仅在 jQuery 每个函数内部执行一次
setInterval only executing once inside of the jQuery each function
我有 3 个 id 为 "cursor" 的 span 元素,但下面的函数只对第一个元素执行。
$(document).ready(function() {
$("#cursor").each(function(i, current) {
console.log("ran");
var $current = $(current);
setInterval(function() {
cursorAnimation($current)
}, 600);
});
});
function cursorAnimation($obj) {
$obj.animate({
opacity: 0
}, 'fast', 'swing').animate({
opacity: 1
}, 'fast', 'swing');
}
ID 选择器将 return 根据 JQuery documentation 只有 0 或 1 个 DOM 元素。您不应为多个元素分配一个 ID。将 ID 更改为 class 并使用 class 选择器 $(".class")
我有 3 个 id 为 "cursor" 的 span 元素,但下面的函数只对第一个元素执行。
$(document).ready(function() {
$("#cursor").each(function(i, current) {
console.log("ran");
var $current = $(current);
setInterval(function() {
cursorAnimation($current)
}, 600);
});
});
function cursorAnimation($obj) {
$obj.animate({
opacity: 0
}, 'fast', 'swing').animate({
opacity: 1
}, 'fast', 'swing');
}
ID 选择器将 return 根据 JQuery documentation 只有 0 或 1 个 DOM 元素。您不应为多个元素分配一个 ID。将 ID 更改为 class 并使用 class 选择器 $(".class")