您正在使用:
var item = layer.get('.rect1'+rect_counter); //dont user "+rec_counter" as this is what binds it to the LAST element
item.on('click', function(){
item.setFill('RED');
});
尝试更基本的解决方法:-------- 这个最适合我 -----------
var itemsList = layer.getChildren(); //gets all shapes in this layer
for(var i=0; i<itemsList.length; i++){
if(itemsList.getName == 'rect1'){ //since you named your rectangles 'rect1' check name
itemsList[i].on('click', function(){
item.setFill('RED');
});
}
};
或者干脆尝试使用:
var item = layer.get('.rect'); //all rectangles, without number
item.on('click', function(e){ // I think you need an 'e' here
item.setFill('RED');
});
// I doubt this will work as ALL your rectangles need to have the same name if you want to .get() them all