你这样做太复杂了。
jQuery(document).ready(function($) {
// run the function for every div
$(".square").each(function(i) {
$('.output').eq(i).val(multiplyColor($(this).val()));
//Show the output to the 1st/2nd/3rd output (that's where eq comes from
//As for the value, I call a function and pass a parameter to do the calculations
//Now if you want to call another function you need to pass the element itself as a parameter;
alertMyId($(this));
});
function alertMyId(elem) {
//elem has now all the same properties as if you were in the "each" function
alert(elem.attr('id'));
}
//Ideally if you just want to calculate something, you should just pass along the value you want to calculate
function multiplyColor(value) {
var color = 3;
return value * color;
}
}); 检查这个小提琴
此外,您不能拥有具有相同 ID 的不同 html 元素。改用类。