gpt4 book ai didi

javascript - 数组中当前单击元素的索引

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

我想知道如何记录数组当前单击元素的索引。

数组:

var cubesmixed = []; 
var cubes;
for(var i = 0; i < 149; i++) {
cubes = paper.rect(Math.floor(Math.random()*2000), Math.floor(Math.random()*2000), 0, 0);
cubesmixed.push(cubes);
}

点击函数:

$(this).click(function() 
{
console.log(index of current clicked element in cubesmixed); 
});

我正在调用 ready() 函数内的所有内容。

提前致谢!

最佳答案

我不确定我是否理解你需要什么(在这个上下文中数组是一个javascript对象,而不是一个DOM元素,所以我不明白为什么有人可以点击它)。

无论如何,请查看 jQuery .index() 方法:http://api.jquery.com/index/

用法:

console.log($(this).index())

希望对你有帮助

更新:

查看完整解决方案的评论

关于javascript - 数组中当前单击元素的索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19422700/

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