触发Javascript循环的所有事件处理程序
我在childCircles数组中有一组圆。我尝试使用嵌套函数添加侦听器。然而,所有事件都被触发,我最终只得到最后一个结果触发Javascript循环的所有事件处理程序,javascript,jquery,html,javascript-events,Javascript,Jquery,Html,Javascript Events,我在childCircles数组中有一组圆。我尝试使用嵌套函数添加侦听器。然而,所有事件都被触发,我最终只得到最后一个结果 var listenEvents = function() { var topRow = document.getElementById('top-row'); var topRowBackground = document.getElementById('top-row-background'); var topRowIcon = document.getElementB
var listenEvents = function() {
var topRow = document.getElementById('top-row');
var topRowBackground = document.getElementById('top-row-background');
var topRowIcon = document.getElementById('top-row-icon');
var images = ["http://placehold.it/1920x50","http://placehold.it/1920x49","http://placehold.it/1919x50","http://placehold.it/1919x49","http://placehold.it/1920x48","http://placehold.it/1918x50"];
for(var i = 0; i < childCircle.length; i++) {
function inner(i) {
return function() {
console.log(event);
console.log(i);
topRowBackground.src = images[i];
topRowIcon.src = $(childCircle[i]).children('img').attr("src");
console.log(topRowBackground.src);
console.log(topRowIcon.src);
topRow.style.display = "block";
}
}
$(document).on('click', childCircle[i], inner(i));
}
var listenEvents=function(){
var topRow=document.getElementById('top-row');
var topRowBackground=document.getElementById('top-row-background');
var topRowIcon=document.getElementById('top-row-icon');
变量图像=[”http://placehold.it/1920x50","http://placehold.it/1920x49","http://placehold.it/1919x50","http://placehold.it/1919x49","http://placehold.it/1920x48","http://placehold.it/1918x50"];
对于(变量i=0;i
仅单击一个圆圈时的控制台消息:
MouseEvent{}
0
MouseEvent{}
一,
MouseEvent{}
二,
MouseEvent{}
三,
MouseEvent{}
四,
MouseEvent{}
五,
您的
childCircle
数组和internal
函数中包含了什么?在这里,查看一个工作示例会非常有帮助。请注意,使用internal(i)
您将立即调用该函数并将其结果分配给click处理程序。childCircle数组实际上包含div元素。内部函数已在代码中。